I'm working on a settings page for an app, and one of the things that I'm looking to do is have a table with a couple of selectable options, some intro text explaining everything at the top, and some additional controls beneath. Understandng that different screen heights can cause some items to be clipped, I'm trying to put all of this into a UIScrollView
. The results so far have been...unintuitive, to say the least.
Here's the code for my view controller with the embedded UIScrollView
:
class ScrollViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var tableView: UITableView!
@IBOutlet var topView: UIView! // Placeholder, intended to represent the explanatory text above the table
@IBOutlet var bottomView: UIView! // Placeholder, intended to represent the additional controls below the table
@IBOutlet var tableViewHeightConstraint: NSLayoutConstraint! // Used to dynamically resize the table
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLayoutSubviews() {
// Dynamically resize the table to fit the number of cells
// Scrolling is turned off on the table in InterfaceBuilder
tableView.frame.size = tableView.contentSize
tableViewHeightConstraint.constant = tableView.frame.height
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20 // Right now we just want enough rows to guarantee the need to scroll
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell? {
cell.textLabel?.text = "Cell content"
tableView.sizeToFit()
return cell
}
return UITableViewCell()
}
/// UIScrollViewDelegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// Disabling horizontal scrolling
scrollView.contentOffset.x = 0.0
print(scrollView.contentOffset.y)
}
}
What I'm currently seeing is that topView
loads in its proper place, and tableView
appears at the specified distance below it per the constraints, with cells running off the bottom of the screen. So far, so good.
Horizontal scrolling is frozen out completely. Good.
When I try to scroll vertically, however, is where it gets weird.
The content itself doesn't move. The vertical scroll bar, however, starts at the screen height and continues to shrink more and more as I endlessly scroll down. The console output shows that the Y index is being remembered and keeps going up well into the tens of thousands if I keep going—yet the content itself never actually gets scrolled. It's as if it thinks the view is ever-growing, and the visible part of the screen merely represents a smaller proportion of the content.
Before I froze out the horizontal scrolling it had no qualms about auto-updating the view and showing things sliding left and right (indeed, it wouldn't scroll vertically at all, near as I could tell). As such, especially since the console shows that contentOffset
is updating automatically, I'm not really sure what else (if anything) I should be setting that isn't already being set.
Does anyone have any insights into how to get it to scroll vertically, and to be aware of the vertical content size so that it doesn't act like it can scroll infinitely (as is the current behavior)?
Thanks!