问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/20444389/can-subviews-resize-their-superview-automatically-with-auto-layout