What are deemed the best practices for setting preferredContentSize? In particular:
- Where is the best place to set it?
- Are there any specific tips for setting it on UINavigationController content view controller?
For context, we have a custom UIPresentationController that darkens/blurs the background and slides up the presented view controller as a sheet from the bottom. It also caps the height of the presented view controller to either the view controller's preferredContentSize.height or 1/3 the containerView's height, whichever is smaller. In this instance, we're using it with a UINavigationController that embeds a UITableViewController, with the preferredContentSize logic implemented in viewWillTransition as follows:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
tableView.layoutIfNeeded()
var sectionRectSize = size
sectionRectSize.height = ceil(min(tableView.contentSize.height, size.height))
preferredContentSize = sectionRectSize
}Unfortunately, this approach has two problems:
- It requires a tableView.layoutIfNeeded() is order to retrieve the correct content size.
- UINavigationController only propogates the preferredContentSize change notification once. After that, further mutations to preferredContentSize on the content view controller do not get propogated back to the custom UIPresentationController.
(The latter one is particularly nasty when iPad rotations get thrown into the mix. We currently do not ship with support for different iPad orientations, but this is something we very much want to address.)
Are there better practices for setting preferredContentSize that can deal with my issues in a holistic way?