0

I'm trng to design a screen for iPad using interface builder. I have an empty view and I added an UIImageView as a wallpaper with 4 constraints:

Leading Space to superview = 0
Trailing Space to superview = 0;
Top Space to superview = 0;
Bottom Space to superview = 0;

In this way, the Image resizes perfectly in landscape mode.

I inserted some debug code in viewDidAppear: and didRotateFromInterfaceOrientation:

NSLog(@"%@", [[UIWindow keyWindow] _autolayoutTrace]);

Only with this elements on the screen I get an ambigous layout output in portrait mode:

*<UIWindow:0x754c210>
|   *<UIView:0x75555f0> - AMBIGUOUS LAYOUT
|   |   *<UIImageView:0x7555650> - AMBIGUOUS LAYOUT

In landscape mode, seems to work fine

*<UIWindow:0x754c210>
|   *<UIView:0x75555f0>
|   |   *<UIImageView:0x7555650>

After that I added more controls and the screen seems to work fine, but I cannot get rid of that ambiguous layout. What I'm doing wrong?

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
Chorche
  • 403
  • 4
  • 16

2 Answers2

1

I guess it sometimes takes time for constraints to "settle down" and not be ambiguous. According to WWDC 2012 video "Best Practices for Mastering Auto Layout," ambiguity can be temporarily tolerated (unlike unsatisfiability, which immediately raises an exception).

If you want to prove to yourself that your constraints are not remaining ambiguous, then create a wrapper for [[UIWindow keyWindow] _autolayoutTrace] and call it after a short delay:

- (void)viewDidAppear:animated
{
    [super viewDidAppear:animated]; 

    [self performSelector:@selector(wrapperForLoggingConstraints) withObject:nil afterDelay:.3];
}

- (void)wrapperForLoggingConstraints
{
    [[UIWindow keyWindow] _autolayoutTrace];
}

You have to create a category on UIWindow in order to get this to work:

@interface UIWindow()

+ (UIWindow *)keyWindow;
- (NSString *)_autolayoutTrace;

@end

I put this category in its own header file, UIWindow_AutoLayoutDebug.h

Where ever I call [[UIWindow keyWindow] _autolayoutTrace] in my app, I import UIWindow_AutoLayoutDebug.h

I learned about calling [[UIWindow keyWindow] _autolayoutTrace] in code from the book "iOS 6 by tutorials", Volume 1, by the raywenderlich.com team. The idea of delaying the call is my own.

bilobatum
  • 8,918
  • 6
  • 36
  • 50
  • You can only call _autolayoutTrace inside an lldb session. It won't compile as you've written it above. You could call the UIView method 'hasAmbiguousLayout', but this tells you about a single view, not its decedents. To do that, one would write a method to recurse through all subviews and call hasAmbiguousLayout on each of them. Remember, for development time only, not for shipping code. – idStar Oct 06 '13 at 13:41
0

I don't know if you are doing anything wrong, but i use Visual Formatting language:

[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentsView]|"
                                            options:0
                                            metrics:nil
                                              views:NSDictionaryOfVariableBindings(contentsView)]];

[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentsView]|"
                                            options:0
                                            metrics:nil
                                              views:NSDictionaryOfVariableBindings(contentsView)]];

I do not see any ambigous layout warnings from tracing it.

Padin215
  • 7,444
  • 13
  • 65
  • 103