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 中设置常量.
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
这篇关于以编程方式更改高度约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:以编程方式更改高度约束
基础教程推荐
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
