关于 + (BOOL)requiresConstraintBasedLayout 方法的理解

关于 requiresConstraintBasedLayout ,从文章中可以得知:

constraint-based layout engages lazily when someone tries to use it (e.g., adds a constraint to a view). 
If you do all of your constraint set up in -updateConstraints, you might never even receive updateConstraints if no one makes a constraint. 
To fix this chicken and egg problem, 
override this method to return YES if your view needs the window to use constraint-based layout.

翻译出来的意思就是基于约束的布局是懒触发的,也就是说只有在使用了约束的情况下,系统才会自动调用 - updateConstraints 方法,如果我们在写代码的时候,把所有约束放在 - updateConstraints 中,那么系统默认是不知道你使用了约束进行布局,这也就是一个鸡生蛋还是蛋生鸡的问题,所以我们需要重写 + requiresConstraintBasedLayout 返回 YES ,告诉系统我们是基于约束进行布局的,这样就可以确保系统会回调 -updateConstraints 方法。

当然如果你的布局不在- updateConstraints 里面,或者部分在- updateConstraints 方法里面,则设置与否并没有什么区别。但是根据苹果官方文档的建议,我们还是尽量将页面布局的代码写在-updateConstraints里面,并且我们在重写-updateConstraints方法之后,需要在代码最后调用[super updateConstraints];

你可能感兴趣的:(关于 + (BOOL)requiresConstraintBasedLayout 方法的理解)