简体   繁体   English

为程序化视图层次结构设置自动布局约束?

[英]Setting up auto layout constraints for programmatic view hierarchy?

I'm creating my view hierarchy programmatically like this: 我正在以编程方式创建我的视图层次结构,如下所示:

UIWindow* window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UIViewController1* viewController1 = [[UIViewController1 alloc] init];
UIViewController2* viewController2 = [[UIViewController2 alloc] init];

UINavigationController* navigationController = [[UINavigationController alloc] init];
[navigationController setViewControllers:@[viewController1, viewController2] animated:NO];

[window setRootViewController:navigationController];
[window makeKeyAndVisible];

The two VC are loaded from XIB's that in both cases use autolayout. 两个VC都是从XIB加载的,在这两种情况下都使用autolayout。 Everything looks good but when I actually do a po [[UIWindow keyWindow] _autolayoutTrace] I get some worrying AMBIGUOUS LAYOUT warnings in the console: 一切看起来都不错,但是当我真正做一个po [[UIWindow keyWindow] _autolayoutTrace]我在控制台中收到一些令人担忧的AMBIGUOUS LAYOUT警告:

*<UIWindow:0xc63bec0>
|   *<UILayoutContainerView:0xd3d79b0> - AMBIGUOUS LAYOUT
|   |   *<UINavigationTransitionView:0xd3d8b60> - AMBIGUOUS LAYOUT
|   |   |   *<UIViewControllerWrapperView:0xd566c00> - AMBIGUOUS LAYOUT
|   |   |   |   *<UIView:0xc66b290> - AMBIGUOUS LAYOUT
|   |   |   |   |   *<UIView:0xc66b0e0> - AMBIGUOUS LAYOUT
|   |   |   |   |   |   *<MKMapView:0xd504800> - AMBIGUOUS LAYOUT

So my question is, how do I get rid of them? 所以我的问题是,如何摆脱它们? Or more generally formulated, how do you go about setting up your window and view hierarchy programmatically using auto layout? 或者更一般地说,如何使用自动布局以编程方式设置窗口和查看层次结构?

I find the documentation very vague on the matter of setting up the window programmatically. 我发现文档在以编程方式设置窗口时非常模糊。 And even though I watched all of the three WWDC videos on the matter I could not get a grip on how to do this. 即使我在这个问题上观看了所有三个WWDC视频,我也无法控制如何做到这一点。

EDIT: It appears as the issues I'm having only relate to the new iOS 7. Since it's under NDA I will move this discussion to the designated Apple Developer Forums. 编辑:它似乎是我刚刚与新的iOS 7有关的问题。因为它是在NDA下我将把这个讨论转移到指定的Apple Developer Forums。

AMBIGUOUS LAYOUT means that you haven't specified enough for Auto Layout to know how to lay out your view. AMBIGUOUS LAYOUT意味着您没有为Auto Layout指定足够的内容来了解​​如何布置视图。 In other words, what you have specified is a bit vague. 换句话说,你指定的内容有点模糊。

This is quite different from broken constraints, where you have two or more constraints that tell Auto Layout to do different things. 这与破坏的约束完全不同,在约束中,您有两个或更多约束,告诉Auto Layout执行不同的操作。

With an ambiguous layout, Auto Layout will try to figure out what you meant to do. 布局模糊不清,Auto Layout将尝试找出您的意图。 Hopefully that will be what you want, but it's not guaranteed. 希望这将是你想要的,但它不能保证。 Hence the warning. 因此警告。

This answer isn't really the place to tell you how to get started. 这个答案实际上不是告诉你如何开始的地方。 But thankfully now more Auto Layout resources are appearing. 但幸运的是,现在出现了更多的自动布局资源。

There's a book iOS Auto Layout Demystified . 有一本书iOS Auto Layout Demystified Although I've bought it, I haven't had a chance to read it yet. 虽然我买了它,但我还没机会读它。 It does look pretty good though. 虽然看起来确实不错。

Also, check out Ole Begemann's excellent article 10 Things You Need To Know About Cocoa Autolayout . 另外,请查看Ole Begemann 关于Cocoa Autolayout的10篇优秀文章。

For the getting started tutorial, have a look at Ray wenderlich's Beginning Auto Layout in iOS 6: Part 1/2 . 有关入门教程,请参阅iOS 6中的Ray wenderlich的Beginning Auto Layout:第1/2部分

Finally, if I can say that there's one Auto Layout thing that gets me every time, it's forgetting to set the setTranslatesAutoresizingMaskIntoConstraints flag to NO for views that I create programmatically that I want to use Auto Layout. 最后,如果我可以说每次都有一个自动布局的东西,那么我忘记将setTranslatesAutoresizingMaskIntoConstraints标志设置为NO ,以便我以编程方式创建我想要使用自动布局的视图。 Keep that in the back of your mind when you see any whacky looking constraint exceptions on the console. 当你在控制台上看到任何看起来很糟糕的约束异常时,请记住这一点。

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

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