0

Masonry这是我用,VFLNSLayoutConstrant方法编写的测试代码:

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]];

我认为这里没有错误,只是一些布局代码。但是,如果我像这样检查 ambiguousLayout:

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

而这三种布局方式都会导致一些问题,错误日志是:

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]
------------------------------------------------------------------

我也做了一些测试,似乎只有跟上,下,左,右,相关属性的布局才会导致这样的问题。

是什么导致了这些问题?真正的答案是什么?

4

0 回答 0