Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(auto layout) container view resizes but subviews don't

I have some problems using auto layouts with container Views (in both cases: with the view containment pattern used from ios5 and a simple container view).

I have a sort of custom Master Detail controller, I can resize the left container after some user action and correlating the right controller have to resize accordingly to the new spaces left not used.

For the "detail" controller I setup the controller with a Xib and using auto layout I have some view inside it. in visual format they would be something like that:

  • V:|[headerView][contentView][footerView]|
  • H:|[headerVieW]
  • H:|[contentView]
  • H:|[footerView]

this is only a representation to undestand the problem.

If i try to resize the detail controller.view frame this expands correctly but the subviews don't.

in the - (void)viewDidLayoutSubviews method of that controller i tried to set the frames manually and that works but it's not the right thing I suppose since I'm using auto layout!

This problem comes back again if I have a generic view and a subview containing image views, if I define that view from xib using autolayout, if I runtime change the frame after the view is setted up subviews (imageviews for edample) don't resizes.

Anyone can help me out?

Thanks!

like image 896
jerrygdm Avatar asked Nov 12 '22 15:11

jerrygdm


1 Answers

Based on your description of the constraints, I think the described behavior is exactly what you told it to do.

Note that your horizontal constraints require the views to be flush with the left side of their superviw. They don't, however, say anything about how they should relate to the right side o the superview. Thus, changing the width of the superview doesn't do anything; they just stay flush with the left size and keep whatever width their internal constraints provide.

If you want the views to resize with their superview, then you need to add constraints on the trailing side. Specifically, your constraints might look like this:

H:|[headerView]|
H:|[contentView]|
H:|[footerView]|

Or maybe you want them to only shrink if the superview is too narrow, but otherwise not stretch all the way across it. Then you might do this:

H:|[headerView]-(>=10)-|
H:|[contentView]-(>=10)-|
H:|[footerView]-(>=10)-|

Anyway, it seems that the root of your problem is that the views aren't tied to the right edge of the superview in any way, and this aren't tied to its width in any way. Adding those constraints should fix the problem.

like image 123
BJ Homer Avatar answered Nov 15 '22 06:11

BJ Homer