Reputation: 2956
My app hierarchy looks something like this
Main view controller (let's call it Controller 1) contains a scrollView, an image and a containerView that points to another viewController and passes data to a tableView.
Controller 1 scroll view is set to be resized correctly with constraints so that containerView can potentially grow in height.
Problem is tableView has data that is loaded dynamically and its size changes, but that does not reflect on containerView. I need my containerView to be notified about tableView new size so it can be resized accordingly too and user can scroll through entire screen, not just within default size of containerView.
I hope that makes sense.
I read about something called preferredContentSizeDidChange but I'm not sure how to implement it properly or whether it is even applicable in my situation.
implementing
override func preferredContentSizeDidChangeForChildContentContainer(container: UIContentContainer) {
super.preferredContentSizeDidChangeForChildContentContainer(container)
print(container)
}
in Controller 1 has not effect
Upvotes: 3
Views: 4950
Reputation: 351
This is an old question, but hopefully it'll help someone! I was able to dynamically resize a container to fit its content by doing the following:
Inside my embedded UITableViewController, I added:
preferredContentSize.height = tableView.contentSize.height
To change the size of the container, I added the embedded view controller as a child of my parent view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? MyTableViewController {
addChildViewController(destination)
}
}
Finally, I added an outlet for the container and its height constraint, and added the following to the parent view controller:
override func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer) {
super.preferredContentSizeDidChange(forChildContentContainer: container)
if let child = container as? MyTableViewController {
containerHeightConstraint.constant = child.preferredContentSize.height
container.updateConstraints()
}
}
Upvotes: 7