简体   繁体   English

iOS Swift-以编程方式更新约束

[英]ios swift - update constraints programmatically

What is the equivalent to pressing the update constraint button in the interface builder auto layout but in code? 与在代码中按界面生成器自动布局中的更新约束按钮等效吗?

I'm updating the size of some subviews and wish to call that update constraints in code. 我正在更新某些子视图的大小,并希望在代码中调用该更新约束。

I think you are looking for: 我认为您正在寻找:

view.setNeedsUpdateConstraints()
view.updateConstraintsIfNeeded()

However, know that the system automatically calls this every layout pass, so you could just use the first line of code unless you need the measurements of the newest constraints. 但是,要知道系统会在每次布局遍历时自动调用此方法,因此除非您需要测量最新约束,否则就可以只使用第一行代码。 From the documentation : 文档中

Whenever a new layout pass is triggered for a view, the system invokes this method to ensure that any constraints for the view and its subviews are updated with information from the current view hierarchy and its constraints. 每当为视图触发新的布局遍历时,系统都会调用此方法以确保使用来自当前视图层次结构及其约束的信息来更新该视图及其子视图的任何约束。 This method is called automatically by the system, but may be invoked manually if you need to examine the most up to date constraints. 该方法由系统自动调用,但是如果您需要检查最新的约束,则可以手动调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM