Dynamically resize Container View to size of embed segue's View Controller contents?

Hi all.


In IB I have dragged a Container View from the object palette to my View Controller ("MainViewController").


I have control-dragged from the Container View to a second View Controller ("EmbeddedViewController"), creating an Embed Segue.


The contents of EmbeddedViewController's view property are subject to change (subviews will be added/removed over time).


Question: how do I make MainViewController dynamically adjust in size to size changes in EmbeddedViewController?


Do I need to do something like add a height Constraint Outlet to MainViewController, and set its constant when the size of EmbeddedViewController changes?


Or is there a simpler way that can be done in IB?


Thanks for any help.

Accepted Reply

Hi SuperWomble,


There is no way to make the resizing happen automatically; you will need some code. If the child view controller updates its preferredContentSize, in response to subviews being added/removed, then the parent view controller (your container) will receive call to its -preferredContentSizeDidChangeForChildContentContainer: method. You can use this opportunity to adjust the height of the Container View.

Replies

Hi SuperWomble,


There is no way to make the resizing happen automatically; you will need some code. If the child view controller updates its preferredContentSize, in response to subviews being added/removed, then the parent view controller (your container) will receive call to its -preferredContentSizeDidChangeForChildContentContainer: method. You can use this opportunity to adjust the height of the Container View.

Thank you Justin, I appreciate the help.

Oh, nice, didn't know about that method. Used height constraint for that.

Does this mean the height has to be set explicitly or is there a way to for autolayout at this point to size the container view to fit that of its contents?