0

MasonryVFLおよびNSLayoutConstrantメソッドで記述された私のテストコードは次のとおりです。

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(@"------------------------------------------------------------------");
    }
}

そして、これら 3 種類のレイアウト方法はすべて何らかの問題を引き起こします。エラー ログは次のとおりです。

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