如何以编程方式更新 UIView 的恒定高度约束? [英] How to update the constant height constraint of a UIView programmatically?

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

问题描述

我有一个 UIView 并且我使用 Xcode Interface Builder 设置了约束.

I have a UIView and I set the constraints using Xcode Interface Builder.

现在我需要以编程方式更新 UIView 实例的高度常量.

Now I need to update that UIView instance's height constant programmatically.

有一个类似于 myUIView.updateConstraints() 的函数,但我不知道如何使用它.

There is a function that goes like myUIView.updateConstraints(), but I don't know how to use it.

推荐答案

从界面构建器中选择高度约束并从中取出一个出口.所以,当你想改变视图的高度时,你可以使用下面的代码.

Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code.

yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()

Method updateConstraints()UIView 的一个实例方法.当您以编程方式设置约束时,这很有帮助.它更新视图的约束.如需更多详细信息,请点击此处.

Method updateConstraints() is an instance method of UIView. It is helpful when you are setting the constraints programmatically. It updates constraints for the view. For more detail click here.

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

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