OC:Masonry约束冲突的查找

代码示例:
#import "ViewController.h"

// Libs
#import "Masonry.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *redView = [[UIView alloc] init];
    redView.mas_key = @"redView";
    redView.backgroundColor = [UIColor redColor];
    
    UIView *blueView = [[UIView alloc] init];
    blueView.mas_key = @"blueView";
    blueView.backgroundColor = [UIColor blueColor];
    
    [self.view addSubview:redView];
    [self.view addSubview:blueView];
    
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.height.equalTo(@50);
        make.right.equalTo(blueView.mas_left);
        // 冲突
        make.width.equalTo(@120);
        make.top.equalTo(@50);
    }];
    
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-30);
        make.height.top.equalTo(redView);
        // 冲突
        make.width.equalTo(@50);
    }];
}

@end
控制台输出:
2019-01-12 15:59:59.962517+0800 OCTestDemo[336:17219] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<MASLayoutConstraint:0x283d8d380 UIView:redView.left == UIView:0x100a0e440.left + 10>",
    "<MASLayoutConstraint:0x283d8d6e0 UIView:redView.right == UIView:blueView.left>",
    "<MASLayoutConstraint:0x283d8d800 UIView:redView.width == 120>",
    "<MASLayoutConstraint:0x283d8daa0 UIView:blueView.right == UIView:0x100a0e440.right - 30>",
    "<MASLayoutConstraint:0x283d8dd40 UIView:blueView.width == 50>",
    "<NSLayoutConstraint:0x283a9a530 UIView:0x100a0e440.width == 375>"
)

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x283d8d800 UIView:redView.width == 120>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
现在就可以很清楚地看到,冲突为:

<MASLayoutConstraint:0x283d8d800 UIView:redView.width == 120>

猜你喜欢

转载自blog.csdn.net/weixin_34161032/article/details/86963199