What's the real meaning of `AmbiguousLayout` in AutoLayout

184 Views Asked by At

Here is my test code written with Masonry, VFL and NSLayoutConstrant methods:

UIView *view1 = [UIView generateView];
UIView *view2 = [UIView generateView];
UIView *view3 = [UIView generateView];
[self.view addSubview:view1];
[self.view addSubview:view2];
[self.view addSubview:view3];

for (UIView *view in @[view1, view2, view3]) {
    NSLog(@"constrants : %@", view.constraints);
}

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view.mas_left).offset(10);
    make.top.equalTo(self.view.mas_top).offset(74);
    make.width.equalTo(@50);
    make.height.equalTo(@50);
}];

view2.translatesAutoresizingMaskIntoConstraints = NO;
for (NSString *vfl in @[@"H:|-10-[view2(50)]", @"V:|-134-[view2(50)]"]) {
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl options:NSLayoutFormatDirectionMask metrics:nil views:NSDictionaryOfVariableBindings(view2)]];
}

view3.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view3 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view3 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:194]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view3 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:50]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:view3 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:50]];

I think it is nothing error here, it's just some layout code. But if i checkout the ambiguousLayout like this:

for (UIView *view in @[view1, view2, view3]) {
    if (view.hasAmbiguousLayout) {
        NSLog(@"view : %@", view);
        NSLog(@"horizontal : %@", [view constraintsAffectingLayoutForAxis:UILayoutConstraintAxisHorizontal]);
        NSLog(@"vertical : %@", [view constraintsAffectingLayoutForAxis:UILayoutConstraintAxisVertical]);
        NSLog(@"------------------------------------------------------------------");
    }
}

And all of these three kinds of layout methods will cause some problem, the error log is :

2017-05-22 13:39:27.185 LayoutExample[13681:810371] view : <UIView: 0x7f9bdd759a60; frame = (0 0; 0 0); layer = <CALayer: 0x608000036be0>>
2017-05-22 13:39:27.185 LayoutExample[13681:810371] horizontal : (
                                                                  "<MASLayoutConstraint:0x6000000a8400 UIView:0x7f9bdd759a60.left == UIView:0x7f9bdd75a0e0.left + 10>",
                                                                  "<MASLayoutConstraint:0x6000000a8640 UIView:0x7f9bdd759a60.width == 50>"
                                                                  )
2017-05-22 13:39:27.186 LayoutExample[13681:810371] vertical : (
                                                                "<MASLayoutConstraint:0x6000000a8580 UIView:0x7f9bdd759a60.top == UIView:0x7f9bdd75a0e0.top + 74>",
                                                                "<MASLayoutConstraint:0x6000000a8700 UIView:0x7f9bdd759a60.height == 50>"
                                                                )
2017-05-22 13:39:27.186 LayoutExample[13681:810371]
------------------------------------------------------------------
2017-05-22 13:39:27.186 LayoutExample[13681:810371] view : <UIView: 0x7f9bdd759c00; frame = (0 0; 0 0); layer = <CALayer: 0x608000037820>>
2017-05-22 13:39:27.186 LayoutExample[13681:810371] horizontal : (
                                                                  "<NSLayoutConstraint:0x600000096260 UIView:0x7f9bdd759c00.left == UIView:0x7f9bdd75a0e0.left + 10>",
                                                                  "<NSLayoutConstraint:0x60000009b170 UIView:0x7f9bdd759c00.width == 50>"
                                                                  )
2017-05-22 13:39:27.187 LayoutExample[13681:810371] vertical : (
                                                                "<NSLayoutConstraint:0x60000009cb10 UIView:0x7f9bdd759c00.top == UIView:0x7f9bdd75a0e0.top + 134>",
                                                                "<NSLayoutConstraint:0x60000009c9d0 UIView:0x7f9bdd759c00.height == 50>"
                                                                )
2017-05-22 13:39:27.209 LayoutExample[13681:810371]
------------------------------------------------------------------
2017-05-22 13:39:27.209 LayoutExample[13681:810371] view : <UIView: 0x7f9bdd759da0; frame = (0 0; 0 0); layer = <CALayer: 0x608000035100>>
2017-05-22 13:39:27.210 LayoutExample[13681:810371] horizontal : (
                                                                  "<NSLayoutConstraint:0x600000095ef0 UIView:0x7f9bdd759da0.left == UIView:0x7f9bdd75a0e0.left + 10>",
                                                                  "<NSLayoutConstraint:0x60000009e2d0 UIView:0x7f9bdd759da0.width == 50>"
                                                                  )
2017-05-22 13:39:27.210 LayoutExample[13681:810371] vertical : (
                                                                "<NSLayoutConstraint:0x60000009df60 UIView:0x7f9bdd759da0.top == UIView:0x7f9bdd75a0e0.top + 194>",
                                                                "<NSLayoutConstraint:0x60000009e3c0 UIView:0x7f9bdd759da0.height == 50>"
                                                                )
2017-05-22 13:39:27.210 LayoutExample[13681:810371]
------------------------------------------------------------------

I also made some tests, It's seems that only to keep up with, down, left, right, the layout of the relevant properties can lead to such problems.

What causes these problem?What's the real answer?

0

There are 0 best solutions below