自动布局会导致UILabel使用不必要的填充来调整大小 [英] Autolayout causing UILabel to resize with unwanted padding

查看:59
本文介绍了自动布局会导致UILabel使用不必要的填充来调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UITableViewCell,在左边有一个图像,在右边有两个UILabel.第一个标签的顶部边缘与图像的顶部水平对齐,第二个标签的底部边缘与图像的底部对齐.

I have a customized UITableViewCell that has an image on the left and two UILabels on the right. The top edge of the first label is horizontally aligned with the top of the image, and the second label has its bottom edge aligned with the bottom of the image.

第二个标签显示更大的内容,我希望它可以垂直调整大小以显示更多内容,但它不应该干扰顶部标签(为此,我在顶部标签和标签之间设置了垂直间距约束内容标签).

The second label displays a larger content and I'm hoping that it could resize vertically to show more content but it should not interfere with the top label(for which I've set a vertical spacing constraint between the top label and the Content Label).

内容"标签的首选宽度设置为自动,并且其内容包含优先级设置为高于垂直间距约束.它的内容抗压缩性优先级较低.

The preferred width for the Content label is set to automatic and its content hugging priority is set higher than the vertical spacing constraint. It has has a lower content compression resistance priority.

如果我输入的某些文本需要多行显示,则内容"标签将调整大小,并且不会干扰顶部标签,但是会在标签的顶部和底部添加这种奇怪的填充.

If I put in some text that requires more than one line to display, the Content label will resize and does not interfere with the top label, but it adds this weird padding to the top and bottom of the label.

如果删除两个标签之间的垂直间距约束,则填充将消失,但内容"标签将超出顶部标签.

If I remove the vertical spacing constraint between the two labels the padding would disappear but the Content label would overrun the top label.

是否可以解决此问题,以使内容"标签的底部与图像的底部对齐,并在不添加这些填充的情况下调整其大小?

Is there a way to fix this so that the Content label would have its bottom aligned with the bottom of the image and resize without adding these paddings?

推荐答案

尝试使用类似 Masonry的库以编程方式实现约束.您将更好地控制UI元素,并更好地了解约束的工作原理.

Try implementing your constraints programmatically using a library like Masonry. You'll have far better control over your UI elements, and a better understanding of how the constraints work.

这篇关于自动布局会导致UILabel使用不必要的填充来调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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