Want to change height of container view according to UIViewController pushed.Currently i dont know how i should update height of container view.
On iOS the UI model is usually such that parent view controls the child view size. For example, each view has initWithFrame constructor, and whoever creates the view (usually parent creates children) is supposed to pass in the size. This is known as "frame-based layout". This system is kind of "top to bottom".
However, Auto Layout lets you "glue" the parent and child views together, and specify relative priorities of those "glue" constraints to the parent view's size and positioning constraints. If the "glue" (and child's compression resistance) wins over the parent's constraints, the parent will be resized accordingly to the child's size.
Using Auto Layout constraints for this is straightforward with 2 custom views (parent and child), but the problem is that it looks like you have a UINavigationController in the middle between your parent and child. One reason that I don't recommend such layout here, is that the navigation bar should normally be on the top of the app screen, and it shouldn't jump up and down when you navigate. Another reason is that it is up to the UINavigationController to decide which way it constraints the child views, and trying to change its habits is not something I'd recommend.
One thing you might try is to implement the navigationController:willShowViewController:animated: delegate method and try to adjust the height of the UINavigationController manually from there depending on which child view controller is pushed.
in your content view controller set the preferredContentSize like the following
override func viewDidLoad() {
super.viewDidLoad()
preferredContentSize = CGSize(width: 400, height: 200)
}
When the child gets added to the parentViewController, the parentViewController gets notified about the prefered size. Following function gets called on the parent
func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer)
In this method you can then change the container to the prefered size of the child.
For example change the height to the prefered height of the child/content
override func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer) {
// heightConstraint is a IBOutlet to your NSLayoutConstraint you want to adapt to height of your content
heigtConstraint.constant = container.preferredContentSize.height
}
maybe you have to pass it through your NavigationViewController. In my example there is no NavigationViewController between the parent and the content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With