Setting up auto layout constraints for programmatic view hierarchy?
Asked Answered
R

1

9

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. Everything looks good but when I actually do a po [[UIWindow keyWindow] _autolayoutTrace] I get some worrying AMBIGUOUS LAYOUT warnings in the console:

*<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.

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.

Rattan answered 27/6, 2013 at 7:53 Comment(0)
D
1

AMBIGUOUS LAYOUT means that you haven't specified enough for Auto Layout to know how to lay out your view. 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.

With an ambiguous layout, Auto Layout will try to figure out what you meant to do. 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 . 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.

For the getting started tutorial, have a look at Ray wenderlich's Beginning Auto Layout in iOS 6: Part 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. Keep that in the back of your mind when you see any whacky looking constraint exceptions on the console.

Diseur answered 11/7, 2013 at 15:10 Comment(1)
I understand the general philosophy and ways of debugging, but my questions relates to how I build the view hierarchy from scratch in code. I cannot hook up my UIWindow to it's super view since it has none.Rattan

© 2022 - 2024 — McMap. All rights reserved.