更改高度约束编程 [英] Change height constraint programmatically
问题描述
我想换一个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
代替。
请注意,使用文本视图的 contentSize
属性来设置文本视图的高度不iOS中7参见<一个工作href=\"http://stackoverflow.com/a/18837714/1239263\">http://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 http://stackoverflow.com/a/18837714/1239263
这篇关于更改高度约束编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!