Can subviews resize their superview automatically with auto layout?

心已入冬 提交于 2019-12-10 13:06:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!