如何根据XIB中的子视图大小调整超视图的高度? [英] How to adjust super view 's height base on subview's size in XIB?

查看:448
本文介绍了如何根据XIB中的子视图大小调整超视图的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xcode 6中,我为自定义视图创建了一个xib(命名为:ViewA,获得了RED背景色),ViewA的xib文件大小为600 * 600,在ViewA中,我放了一个子视图labelB(得到了一个绿色)其中,labelB的numberOfLines = 0,所以labelB'高度是可变的,我希望ViewA的高度根据labelB的高度而改变(例如ViewA.bottom = labelB.bottom + 10),我已经把它固定了labelB的顶部,底部,尾部,通向ViewA,但它仍然没有!工作,ViewA的高度总是600,无论标签的高度是多少。如何在汽车布局中实现这一目标?谢谢

In xcode 6 ,i create a xib for a custom view (named: ViewA,got a RED background color) ,and ViewA's xib got a file size 600*600, in ViewA ,I put a subview labelB (got a GREEN background color )in it ,and labelB's numberOfLines = 0 ,so labelB'height is variable , I want the ViewA 's height changed based on labelB's height (e.g ViewA.bottom = labelB.bottom + 10), and I have already pin the labelB's top,bottom,trailing,leading to ViewA, but it still doesn't !work ,the ViewA's height is always 600 ,no matter what label's height is . How can i achieve this goal in auto layout? thanks

推荐答案

使用自动布局,这是您需要做的事情:

Using Auto Layout, here's what you need to do:


  1. 确保您没有为任何子视图添加固定宽度和/或高度约束(取决于您想要动态调整的尺寸)。我们的想法是让每个子视图的内在内容大小决定子视图的高度。 UILabel 带有4个自动隐式约束,它们(小于所需优先级)尝试将标签的框架保持在适合所有文本所需的确切大小。

  1. Make sure you aren't adding fixed width and/or height constraints to any of your subviews (depending on which dimension(s) you want to dynamically size). The idea is to let the intrinsic content size of each subview determine the subview's height. UILabels come with 4 automatic implicit constraints which will (with less than Required priority) attempt to keep the label's frame at the exact size required to fit all the text inside.

确保每个标签的边缘严格连接(具有必需的优先级约束)到彼此的边缘及其超视图。你想确保如果你想象其中一个标签的尺寸越来越大,这将迫使其他标签为它腾出空间,最重要的是迫使超视图扩展。

Make sure that the edges of each label are connected rigidly (with Required priority constraints) to the edges of each other and their superview. You want to make sure that if you imagine one of the labels growing in size, this would force the other labels to make room for it and most importantly force the superview to expand as well.

仅向超级视图添加约束以设置其位置,而不是大小(至少不是您想要动态调整大小的维度)。请记住,如果正确设置内部约束,其大小将由所有子视图的大小决定,因为它的边缘以某种方式连接到它们的边缘。

Only add constraints to the superview to set its position, not size (at least, not for the dimension(s) you want it to size dynamically). Remember that if you set the internal constraints up correctly, its size will be determined by the sizes of all the subviews, since its edges are connected to theirs in some fashion.

确保在应用任何约束之前更改为查看 translatesAutoresizingMaskIntoConstraints 属性值为

Make sure you change to view's translatesAutoresizingMaskIntoConstraints property value to NO before applying any constraints.

就是这样。您不需要调用 sizeToFit systemLayoutSizeFittingSize:来实现此功能,只需加载您的视图并设置文本应该是它。系统布局引擎将为您进行计算以解决您的约束。 (如果有的话,您可能需要在超级视图上调用 setNeedsLayout ...但这不应该是必需的。)

That's it. You don't need to call sizeToFit or systemLayoutSizeFittingSize: to get this to work, just load your views and set the text and that should be it. The system layout engine will do the calculations for you to solve your constraints. (If anything, you might need to call setNeedsLayout on the superview...but this shouldn't be required.)

您可以查看原始答案此处

这篇关于如何根据XIB中的子视图大小调整超视图的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆