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

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

问题描述

我看到约束设置不同的例子。一些设置它们 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 是不断变化的约束值是有用的,例如如果一些约束是依赖于屏幕的方向(我知道,这是一个不好的做法),你可以改变它的在这个方法中。

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.

viewDidLoad中会话介绍自动布局iOS和OS X的(WWDC 2012)期间表现出增加的限制,无法启动39:22。我认为这是那些演讲时说,但在文件中没有土地的事情之一。

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.

更新:我注意到在<一个建立约束的提href=\"https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html#//apple_ref/doc/uid/TP40007457-CH10-SW36\">Resource在视图控制器管理的:

如果您preFER以编程方式创建视图,而不是使用
  故事板,您可以通过重写这样做你的视图控制器的的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,如果您使用的是自动布局,分配足够的约束,以每
  您刚才创建的视图来控制的位置和大小的
  意见。否则,实施 viewWillLayoutSubviews
   viewDidLayoutSubviews 的方法来调整子视图的帧
  视图层次结构。请参见调整视图控制器的意见。

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年苹果给的新解释, updateConstraints updateViewConstraints 推荐用法:

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.

您所有的初始约束设置的最好应发生的界面生成器中。

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.

此外,它是pretty直白的只是改变的约束,当你发现需要做;然而,如果从其他code,它是关系到它采取的逻辑分开,你将它移动到该得到在以后执行一个单独的方法,你的code成为了很多困难跟随,所以它会更难为您维护,这将是一个困难得多其他人理解。

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?

好了,把它归结为性能。

Well, it boils down to performance.

如果你发现只是改变你的地方是制约太慢,然后更新的限制也许能帮助你。

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天全站免登陆