以编程方式创建视图时,我应该在哪里设置自动布局约束 [英] Where should I be setting autolayout constraints when creating views programmatically

查看:68
本文介绍了以编程方式创建视图时,我应该在哪里设置自动布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了设置约束的不同示例.有些人在viewDidLoad/loadView中设置了它们(添加了子视图之后).其他人在方法updateViewConstraints中设置它们,该方法由viewDidAppear调用.

I see different examples where constraints are set. Some set them in viewDidLoad / loadView (after the subview was added). Others set them in the method updateViewConstraints, which gets called by viewDidAppear.

当我尝试在updateViewContraints中设置约束时,布局可能会出现跳动,例如在显示视图之前稍有延迟.另外,如果我使用此方法,是否应该先清除现有约束,即[self.view [removeConstraints:self.view.constraints]?

When I try setting constraints in updateViewContraints there can be a jumpiness to the layout, e.g. slight delay before the view appears. Also, if I use this method, should I clear out existing constraints first i.e. [self.view [removeConstraints:self.view.constraints]?

推荐答案

我在viewDidLoad/loadView中设置了约束(我的目标是iOS> = 6). updateViewConstraints对于更改约束的值很有用,例如如果某些约束取决于屏幕的方向(我知道,这是一种不好的做法),则可以使用此方法更改其constant.

I set up my constraints in viewDidLoad/loadView (I'm targeting iOS >= 6). updateViewConstraints is useful for changing values of constraints, e.g. if some constraint is dependent on the orientation of the screen (I know, it's a bad practice) you can change its constant in this method.

在会话"iOS和OS X的自动布局简介" (WWDC 2012)(从39:22开始)中显示了在viewDidLoad中添加约束.我认为这是在演讲中说过的事情之一,但没有出现在文档中.

Adding constraints in viewDidLoad is showed during the session "Introduction to Auto Layout for iOS and OS X" (WWDC 2012), starting from 39:22. I think it's one of those things that are said during lectures but don't land in the documentation.

更新:我注意到在

如果您希望以编程方式创建视图,而不是使用 情节提要,您可以通过覆盖视图控制器的loadView 方法.您对该方法的实现应执行以下操作:

If you prefer to create views programmatically, instead of using a storyboard, you do so by overriding your view controller’s loadView method. Your implementation of this method should do the following:

(...)

3.如果使用自动版式,请为每个布局分配足够的约束 您刚刚创建的用于控制位置和尺寸的视图 观看次数.否则,实施viewWillLayoutSubviewsviewDidLayoutSubviews调整子视图框架的方法 视图层次结构.请参阅调整视图控制器的视图大小".

3.If you are using auto layout, assign sufficient constraints to each of the views you just created to control the position and size of your views. Otherwise, implement the viewWillLayoutSubviews and viewDidLayoutSubviews methods to adjust the frames of the subviews in the view hierarchy. See "Resizing the View Controller’s Views."

更新2 :在WWDC 2015期间 Apple提供了新的解释 updateConstraintsupdateViewConstraints推荐用法:

UPDATE 2: During WWDC 2015 Apple gave a new explanation of updateConstraints and updateViewConstraints recommended usage:

真的,所有这些都是视图有机会在下一次布局通过时及时更改约束的一种方法,但是通常并不需要.

Really, all this is is a way for views to have a chance to make changes to constraints just in time for the next layout pass, but it's often not actually needed.

理想情况下,所有初始约束设置都应在Interface Builder内部进行.

All of your initial constraint setup should ideally happen inside Interface Builder.

或者,如果您真的发现需要以编程方式分配约束,那么诸如viewDidLoad之类的地方会更好.

Or if you really find that you need to allocate your constraints programmatically, some place like viewDidLoad is much better.

更新约束实际上仅用于需要定期重复的工作.

Update constraints is really just for work that needs to be repeated periodically.

此外,当您发现需要更改约束时,这很简单.相反,如果将这种逻辑与其他与之相关的代码分开,然后将其移至一个单独的方法中,然后在以后执行该方法,则代码将变得难以遵循,因此维护起来将更加困难,其他人很难理解.

Also, it's pretty straightforward to just change constraints when you find the need to do that; whereas, if you take that logic apart from the other code that's related to it and you move it into a separate method that gets executed at a later time, your code becomes a lot harder to follow, so it will be harder for you to maintain, it will be a lot harder for other people to understand.

那么您何时需要使用更新约束?

So when would you need to use update constraints?

好吧,它归结为性能.

如果您发现仅更改约束就太慢了,那么更新约束可能可以帮助您.

If you find that just changing your constraints in place is too slow, then update constraints might be able to help you out.

事实证明,在更新约束中更改约束实际上比在其他时间更改约束要快.

It turns out that changing a constraint inside update constraints is actually faster than changing a constraint at other times.

之所以这样做,是因为引擎能够将在此传递中发生的所有约束更改视为批处理.

The reason for that is because the engine is able to treat all the constraint changes that happen in this pass as a batch.

这篇关于以编程方式创建视图时,我应该在哪里设置自动布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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