ios 以编程方式更改高度约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20930000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change height constraint programmatically
提问by mxch
I want to change the height constraint of a UITextView programmatically so I set the constraint as an outlet in my controller like this:
我想以编程方式更改 UITextView 的高度约束,因此我将约束设置为控制器中的出口,如下所示:
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *descriptionHeightConstraint;
To change it I do the following:
要更改它,我执行以下操作:
self.descriptionHeightConstraint.constant = self.description.contentSize.height;
This works if I do it in viewDidAppearbut 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 viewWillAppearbut didn't work there, the height doesn't change. Calling setNeedsUpdateConstraintsafter changing the constraint didn't work either.
如果我在viewDidAppear 中这样做,这会起作用,但问题在于我可以看到显示视图后高度如何变化,这对用户来说不是很友好,所以我尝试在viewWillAppear 中这样做,但没有在那里工作,高度没有改变。在更改约束后调用setNeedsUpdateConstraints也不起作用。
Why is working in viewDidAppear and not in viewWillAppear? Any workaround?
为什么在 viewDidAppear 中工作而不是在 viewWillAppear 中工作?任何解决方法?
Thanks in advance!
提前致谢!
采纳答案by bilobatum
Try setting the constant in viewDidLayoutSubviews
instead.
尝试设置常量viewDidLayoutSubviews
。
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
请注意,使用文本视图的contentSize
属性设置文本视图的高度在 iOS 7 中不起作用。请参阅https://stackoverflow.com/a/18837714/1239263