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.
Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions.
Select the items you want to align, and then click the Align tool. Interface Builder presents a popover view containing a number of possible alignments. Select the options for aligning the selected views, and click the Add Constraints button. Interface Builder creates the constraints needed to ensure those alignments.
To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen. Then click the “Add 2 Constraints” button. Run the application.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With