[英]How to adjust the parent view's height according to its subviews using auto layout
I have cutomized a view, whose subviews's height will dynamically change(I'm changing the height by changing the constraint). 我已经对一个视图进行了裁剪,其子视图的高度将动态更改(我通过更改约束来更改高度)。 My intention is to let the view's height adjust to its subviews' height, so I won't care for anything, and that's what autolayout is for. 我的意图是让视图的高度调整为其子视图的高度,因此我将不需要任何东西,这就是自动布局的目的。
In practise, there is no way to add any constraints to the root view in IB. 实际上,无法在IB的根视图中添加任何约束。 And I found I can still change the frame of the root view, which appears to be able to solve my current problem. 我发现我仍然可以更改根视图的框架,这似乎能够解决我当前的问题。 But I don't think it's recommended to compound auto layouts and frames. 但是我不建议您将自动布局和框架混合使用。
So, any other suggestions? 那么,还有其他建议吗?
It's perfectly fine to have some views using the old system of layout (ie, autoresizing and frame setting) while other views are using Auto Layout directly. 最好使用旧版式的系统(即自动调整大小和框架设置)来创建一些视图,而其他视图直接使用自动版式。 The important thing to remember is to set the translatesAutoresizingMaskIntoConstraints
property to NO
on views instantiated in code that are using Auto Layout. 要记住的重要事情是,在使用“自动布局”的代码中实例化的视图上,将translatesAutoresizingMaskIntoConstraints
属性设置为“ NO
”。
However, you can't use Auto Layout on a view controller's root view. 但是,不能在视图控制器的根视图上使用“自动布局”。
Using Auto Layout, subviews can dictate the size of their superview–but this won't work if the superview is using autoresizing and frame setting. 使用“自动布局”,子视图可以决定其超级视图的大小,但是如果超级视图使用自动调整大小和框架设置,则此功能将无效。
I don't know if the root view you're working with is the root view of a scene's main view controller or the root view of a child view controller. 我不知道您使用的根视图是场景主视图控制器的根视图还是子视图控制器的根视图。 If the former, I don't recommend adjusting the root view's frame in iOS 7. Just let the root view fill the whole screen (which is its default behavior). 如果是前者,我不建议在iOS 7中调整根视图的框架。只需让根视图填充整个屏幕(这是其默认行为)。
Subviews have their size dictated by their superview, not the other way around. 子视图的大小取决于其父视图,而不是相反。 For your special case, I would make the subview delegate its height changes back to the superview. 对于您的特殊情况,我将使子视图委托其高度更改回到超级视图。 While you could then adjust the rect of the superview (and avoid changing the subview's height altogether), I would do this: 然后,您可以调整超级视图的矩形(并避免完全更改子视图的高度),我可以这样做:
You shouldn't have to modify the height of the child view, because the constraints will do that for you. 您不必修改子视图的高度,因为约束将为您完成此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.