使用Autolayout时自动调整UILabel大小 [英] Auto sizing UILabel when using Autolayout

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

问题描述

我正在使用autolayout(NSLayoutConsraint)创建一个xib。有没有办法使用sizeToFit或类似的东西使UILabel适合其内容?

I'm creating a xib using autolayout (NSLayoutConsraint). Is there a way to use sizeToFit or something similar to make a UILabel fit its contents?

我尝试设置大于或等于x的约束,并尝试将高度设置为低于此值。但是,它会一直覆盖height属性。当你有NSLayoutConstraints时,sitToFit似乎不起作用。

I've tried setting a constraint of "greater than or equal to x" and tried setting the height to be below this. However, it keeps overwriting the height property. sitToFit doesn't seem to work when you have NSLayoutConstraints.

如何才能使标签大小合适?

How can I get the label to size appropriately?

推荐答案

根据宽度和高度限制制作IBOutlet,并使用 sizeToFit 更改常量属性,并调用 needsUpdateConstraints

Make an IBOutlet for your width and height constraints, and use sizeToFit to change their constant property, and call needsUpdateConstraints

这篇关于使用Autolayout时自动调整UILabel大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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