1、 //代码约束 NSLayoutConstraint
UIView *blue = [UIView new];
blue.backgroundColor = [UIColor blueColor];
blue.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blue];
UIView *red = [UIView new];
red.backgroundColor = [UIColor yellowColor];
red.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:red];
//高度
NSLayoutConstraint *blueHeight = [NSLayoutConstraint constraintWithItem:blue attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:40];
[blue addConstraint:blueHeight];
NSLayoutConstraint *redHeight = [NSLayoutConstraint constraintWithItem:red attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blue attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
[self.view addConstraint:redHeight];
//上
NSLayoutConstraint *blueTop = [NSLayoutConstraint constraintWithItem:blue attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
[self.view addConstraint:blueTop];
NSLayoutConstraint *redTop = [NSLayoutConstraint constraintWithItem:red attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blue attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
[self.view addConstraint:redTop];
//左
NSLayoutConstraint *blueLeft = [NSLayoutConstraint constraintWithItem:blue attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
[self.view addConstraint:blueLeft];
NSLayoutConstraint *redLeft = [NSLayoutConstraint constraintWithItem:red attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
[self.view addConstraint:redLeft];
//右
NSLayoutConstraint *blueRight = [NSLayoutConstraint constraintWithItem:blue attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
[self.view addConstraint:blueRight];
NSLayoutConstraint *redwidth = [NSLayoutConstraint constraintWithItem:red attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
[self.view addConstraint:redwidth];
2.VFL-