AutoLayout将UILabel高度设置为零 [英] AutoLayout setting UILabel height to zero

查看:111
本文介绍了AutoLayout将UILabel高度设置为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UILabel 用于其他视图中的项目描述,所有这些都使用Interface Builder中的约束进行布局 - 您可以在下面的图像中看到所有相关约束。行数也设置为 0

I have a UILabel for item description amongst other views, all laid out using constraints in Interface Builder - you can see all relevant constraints in the image below. The number of lines is also set to 0.

我没有设置高度限制因为我希望 UILabel 根据它包含的文本调整大小。相反,发生的事情就在

I haven't set the height constraint because I want the UILabel to resize based on the text it contains. Instead, what happens is right after

[self.view layoutIfNeeded];

被调用, UILabel的高度设置为 0 。即使我没有将其他文本设置为 UILabel ,它也会在Interface Builder中设置默认值项目描述

is called, the height of the UILabel gets set to 0. Even if I don't set other text to the UILabel, it has a default value of Item description set in Interface Builder.

上面的项目标题标签设置方式相同,但是那个没有被压扁到 0 ,所以我是有点困惑。

The item title label above is set the same way, but that one doesn't get squashed to 0, so I'm a bit confused.

任何人都有过这种行为的经历吗?

Anyone had any experience with such behaviour?

推荐答案

我设法通过将 UILabel 垂直压缩阻力优先级设置为来解决此问题Interface Builder中的1000 (默认 750 )。

I managed to solve it by setting the UILabel's vertical compression resistance priority to 1000 (default 750) in Interface Builder.

由于我的视图嵌入在另一个视图中,而父视图的底部依赖于最低子视图的底部,我只推测 UILabel 在这个过程中受到挤压阐述意见。可能在链条的某个地方玩其他约束的优先级会产生相同的结果,但我无法成功地做到这一点。然而,上面的解决方案是有效的,这在我的情况下已经足够了。

Since my views are embedded in another view, and the parent view's bottom is dependent on the bottom of the lowest child view, I only speculate that the UILabel without a height constraint was getting squeezed in the process of laying out the views. Probably playing with priorities of other constraints somewhere down the chain would have yielded the same result, but I wasn't able to do it successfully. The solution above, however, worked, which is good enough in my case.

希望这有助于某人。

这篇关于AutoLayout将UILabel高度设置为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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