Change height constraint programmatically

扶醉桌前 提交于 2019-11-27 14:24:23

问题


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;

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.

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

Thanks in advance!


回答1:


Try setting the constant in viewDidLayoutSubviews instead.

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



来源:https://stackoverflow.com/questions/20930000/change-height-constraint-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!