I am trying this method of using UIScrollView and UIStackView to add multiple child VCs.
The first childVC in my vertical stackView is a straightforward set of labels, and has height of around 150 points.
The second childVC under the first childVC is a collectionView with diffable data source and compositional layout style to make it look like a tableView, with labels and textview in each cell. This second childVC will grow in content as users add more data.
What I want is either:
- Set the first childVC pinned to the top so it does not scroll; or
- pin the first childVC with the second childVC so they scroll together.
Right now, when I scroll just where the second childVC is, then the second childVC will scroll, leaving the first childVC where it was (at the top of the view). But the second childVC won't scroll to the bottom of the list (the cells get caught behind my tabBarController). So if I want to scroll all the way to the bottom to see the full list, i have to scroll from the fist childVC.
But the things get all funky because two scroll indicators appear and if i want to scroll up and down, i have to play around with the scroll indicator, and this whole scrolling experience becomes a nightmare
Both child VCs have separate view subclass. So for example:
class FirstChildVC: UIViewController {
var firstChildView: FirstChildView!
override func viewDidLoad() {
super.viewDidLoad()
firstChildView = FirstChildView()
view.addSubView(firstChildView)
}
private func configure() {
firstChildView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
firstChildView.topAnchor.constraint(equalTo: view.topAnchor),
firstChildView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: view.frame.height),
firstChildView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
firstChildView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
firstChildView.heightAnchor.constraint(equalToConstant: view.frame.height)
])
}
And the second childVC is pretty much the same, except the constraints are:
func configure() {
secondChildView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
secondChildView.topAnchor.constraint(equalTo: view.topAnchor, constant: 150),
secondChildView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
secondChildView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
secondChildView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
secondChildView.heightAnchor.constraint(equalToConstant: view.frame.height)
])
}
If I don't set the topAnchors and/or heightAnchors of the childViews like that, the stackView overlaps.. which I find weird because I thought the whole point of stackView is to stack horizontally or vertically, and each arrangedSubView that is added has its own contentView.
How can I fix it so I can scroll the first childVC and second childVC together, instead of each having its own scroll indicator?
edit: this is a screen recording of what i meant by funky scroll.
edit2: I found out that the problem with this funky behaviour is because my second childVC dynamically grows in height as users add more data. Right now, the second childVC's view's constraints are set to the view's anchors, and height constraint is set to view.frame.height
because not setting a height constraint doesnt load the view... i dont know why.
But, if i set the height constraint to something crazy like view.frame.height * 5
then i get the result that i wanted, as in there is no separate scroll indicator for second childVC. But because this VC changes in height dynamically, setting a constant height is a terrible idea, and i want the height to grow as the data is added/removed. Any ideas? Should I post a separate question for this? (new to stack overflow.. sorry)