xcode 如何根据XIB中子视图的大小调整超级视图的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27014273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to adjust super view 's height base on subview's size in XIB?
提问by ximmyxiao
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
在 xcode 6 中,我为自定义视图创建了一个 xib(名为:ViewA,背景颜色为红色),而 ViewA 的 xib 的文件大小为 600*600,在 ViewA 中,我放置了一个子视图标签 B(背景颜色为绿色)其中,labelB 的 numberOfLines = 0,所以 labelB'height 是可变的,我希望 ViewA 的高度根据 labelB 的高度改变(例如 ViewA.bottom = labelB.bottom + 10),并且我已经固定了 labelB 的顶部,底部,尾随,通向 ViewA,但它仍然不起作用!无论标签的高度是什么,ViewA 的高度始终为 600。我怎样才能在自动布局中实现这个目标?谢谢
回答by Anuj Rajput
Using Auto Layout, here's what you need to do:
使用自动布局,您需要执行以下操作:
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.
UILabel
s 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.
Make sure you change to view's
translatesAutoresizingMaskIntoConstraints
property value toNO
before applying any constraints.
确保您没有向任何子视图添加固定宽度和/或高度约束(取决于您想要动态调整大小的维度)。这个想法是让每个子视图的内在内容大小决定子视图的高度。
UILabel
s 带有 4 个自动隐式约束,它们将(优先级低于 Required )尝试将标签的框架保持在适合其中所有文本所需的确切大小。确保每个标签的边缘与彼此的边缘及其父视图严格连接(具有必需的优先级约束)。您想确保如果您想象其中一个标签的大小不断增长,这将迫使其他标签为其腾出空间,最重要的是,也将迫使超级视图扩展。
仅向超级视图添加约束以设置其位置,而不是大小(至少,不是您希望它动态调整大小的维度)。请记住,如果您正确设置了内部约束,它的大小将由所有子视图的大小决定,因为它的边缘以某种方式连接到它们的边缘。
在应用任何约束之前,请确保将视图的
translatesAutoresizingMaskIntoConstraints
属性值更改为NO
。
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.)
就是这样。您不需要调用sizeToFit
或systemLayoutSizeFittingSize:
让它工作,只需加载您的视图并设置文本,就可以了。系统布局引擎将为您进行计算以解决您的约束。(如果有的话,您可能需要调用setNeedsLayout
超级视图......但这不应该是必需的。)
You can check the original answer here
您可以在此处查看原始答案