Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can subviews resize their superview automatically with auto layout?

Tags:

ios

autolayout

If you have a view with no constraints on it, and that view has a subview with left, right, top and bottom constraints of equal size, if the subview's size is increased is it possible for the superview's size to automatically increase to accommodate these constraints?

Thanks.

like image 701
Darren Findlay Avatar asked Dec 07 '13 17:12

Darren Findlay


2 Answers

Yes, you can do that, and it can be done without any code or subclassing. For instance, if you have a label embedded in a view (with numberOfLines = 0), and it has constraints to to all the sides of the superview, as well as a width constraint, the label, and its superview will expand vertically if you put a large string in the label. The superview should still need to have some constraints, like one to the top and left side of its superview, but no size constraints.

like image 186
rdelmar Avatar answered Nov 20 '22 18:11

rdelmar


This can be done if the superview is a custom subclass of UIView. You can override intrinsicContentSize to return a size related to the size of your super view. Then, when the size of the subview changes, it can invalidate the intrinsic content size. I typically do this in the layoutSubviews part of the superview.

like image 26
hukir Avatar answered Nov 20 '22 18:11

hukir