使用自动布局动态更改子视图后调整超级视图的大小 [英] resize superview after subviews change dynamically using autolayout

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

问题描述

看在上帝的份上,我无法理解这个调整大小的超级视图的窍门.

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

我有一个带有 4 个 UILabelsUIView *superview.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:,传递UILayoutFittingCompressedSizeUILayoutFittingExpandedSize.

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:,传递UILayoutFittingCompressedSizeUILayoutFittingExpandedSize.我已经阅读了很多关于堆栈的提示,但最终什么也没做.

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.

我需要在特定的地方重新计算超级视图的约束.Maby 在其控制器类中将高度设置为@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.

我正在抓我的头发..帮助

I am scratching my hair of.. help

推荐答案

如果您使用自动布局,您需要执行以下操作:

If you're 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.

就是这样.你不需要调用 sizeToFitsystemLayoutSizeFittingSize: 来让它工作,只需加载你的视图并设置文本就可以了.系统布局引擎将为您进行计算以解决您的约束.(如果有的话,您可能需要在超级视图上调用 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.)

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

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