调整后的子视图的SuperView变化动态地使用自动布局 [英] resize superview after subviews change dynamically using autolayout

查看:211
本文介绍了调整后的子视图的SuperView变化动态地使用自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能在上帝的爱挂起这个调整的SuperView的。

I cant for the love of god the the hang of this resizing superview.

我有一个 UIView的*上海华 4个 UILabels 。 2功能为2人的头。

I have a UIView *superview with 4 UILabels. 2 function as header for the 2 others.

在所有4个内容是动态的,从数据库中来了。

The content in all 4 are dynamic coming from database.

SizeToFit VS SizeThatFits:(CGSize) VS 的UIView systemLayoutSizeFittingSize:,无论是通过 UILayoutFittingCom pressedSize UILayoutFittingExpandedSize

SizeToFit vs SizeThatFits:(CGSize) vs UIView systemLayoutSizeFittingSize:, passing either UILayoutFittingCompressedSize or UILayoutFittingExpandedSize.

我编程使用自动布局和设置了上海华高度等于或大于一个虚拟号码。

I use autolayout programatically and have set the superview height to be equal or greater to a dummy number.

在哪里?如何使用这些 SizeToFit VS sizeThatFits:(CGSize) VS UIView的systemLayoutSizeFittingSize:,无论是通过 UILayoutFittingCom pressedSize UILayoutFittingExpandedSize 。我已经读了很多在这里堆技巧,但最终什么也没有。

where and how do I use these SizeToFit vs sizeThatFits:(CGSize) vs UIView systemLayoutSizeFittingSize:, passing either UILayoutFittingCompressedSize or UILayoutFittingExpandedSize. I have read a lot of tips here on stack but ended up with nothing.

我是否需要重新计算放在特定的上海华的约束。莫比设定的高度是'@ property`在其控制器类和删除,并重新进行添加呢?
ATM我试图把事事处处,然后一些。尽管如此,我得到的虚拟高度和文字外飘大小相同的最终结果。即使后子视图设置clipsToBound。

DO I need to recalculate the constraints for the superview somewhere specific. Maby setting the height to be ´@property` in its controller class and remove and readd it? Atm I have tried to put everything everywhere and then some. Still I get the same size end result with the dummy height and text floating outside. Even after setting clipsToBound on subview.

我抓我的头发..帮助

推荐答案

如果您正在使用自动布局,这里就是你需要做什么:

If you're using Auto Layout, here's what you need to do:


  1. 请确保您不添加固定宽度和/或高度限制你的任何子视图(取决于你想要动态大小的维度(S))。这样做是为了让每个子视图的固有内容大小确定子视图的高度。 的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.

确保每个标签的边缘被刚性连接(使用优先级要求的约束)到对方和自己的SuperView的边缘。你要确保,如果你想象中不断扩大规模的一个标签,这将迫使其他标签,以腾出空间,最重要的迫使上海华扩大为好。

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.

就是这样。你并不需要调用 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.)

这篇关于调整后的子视图的SuperView变化动态地使用自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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