以编程方式更改高度约束 [英] Change height constraint programmatically

查看:27
本文介绍了以编程方式更改高度约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式更改 UITextView 的高度约束,因此我将约束设置为控制器中的插座,如下所示:

I want to change the height constraint of a UITextView programmatically so I set the constraint as an outlet in my controller like this:

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *descriptionHeightConstraint;

要更改它,我执行以下操作:

To change it I do the following:

self.descriptionHeightConstraint.constant = self.description.contentSize.height;

如果我在 viewDidAppear 中执行此操作,但问题是我可以看到显示视图后高度如何变化,这对用户不是很友好,所以我尝试在 中执行此操作>viewWillAppear 但没有在那里工作,高度没有改变.更改约束后调用 setNeedsUpdateConstraints 也不起作用.

This works if I do it in viewDidAppear but the problem with this is that I can see how the height changes after the view displayed which is not very user friendly so I tried doing it in viewWillAppear but didn't work there, the height doesn't change. Calling setNeedsUpdateConstraints after changing the constraint didn't work either.

为什么在 viewDidAppear 中工作而不在 viewWillAppear 中工作?有什么解决方法吗?

Why is working in viewDidAppear and not in viewWillAppear? Any workaround?

提前致谢!

推荐答案

尝试在 viewDidLayoutSubviews 中设置常量.

Try setting the constant in viewDidLayoutSubviews instead.

请注意,使用文本视图的 contentSize 属性设置文本视图的高度在 iOS 7 中不起作用.请参阅 https://stackoverflow.com/a/18837714/1239263

Note that using the text view's contentSize property to set the text view's height does not work in iOS 7. See https://stackoverflow.com/a/18837714/1239263

这篇关于以编程方式更改高度约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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