代码效果:
- (void)viewDidLoad
{
[super viewDidLoad];
//1.添加视图
UIView *blueView =[[UIView alloc]init];
blueView.backgroundColor =[UIColor blueColor];
//要先禁止autoresizing功能
blueView.translatesAutoresizingMaskIntoConstraints =NO;
[self.view addSubview:blueView];
UIView *redView =[[UIView alloc]init];
redView.backgroundColor =[UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints =NO;
[self.view addSubview:redView];
//*************************************************2.蓝色约束*********************
CGFloat margain =20;
//2.1蓝色视图左边约束
NSLayoutConstraint *blueLeft =[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:margain];
[self.view addConstraint:blueLeft];
//2.2蓝色视图右边约束
NSLayoutConstraint *blueReight =[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant: -margain];
[self.view addConstraint:blueReight];
//2.3蓝色视图顶部约束
NSLayoutConstraint *blueTop =[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:margain];
[self.view addConstraint:blueTop];
//2.4蓝色视图高度约束
NSLayoutConstraint *blueHeight =[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:margain*2];
[blueView addConstraint:blueHeight];
//*************************************************3.红色约束*********************
//3.红色约束
//3.1红色视图右边约束
NSLayoutConstraint *RedRight =[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];
[self.view addConstraint:RedRight];
//3.2红色视图高度边约束
NSLayoutConstraint *RedHight =[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];
[self.view addConstraint:RedHight];
//3.3红色视图左边约束
NSLayoutConstraint *RedLeft =[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0];
[self.view addConstraint:RedLeft];
//3.4红色视图顶部边约束
NSLayoutConstraint *RedTop =[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:margain];
[self.view addConstraint:RedTop];
}
其中 NSLayoutConstraint *blueLeft =[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:margain];
[self.view addConstraint:blueLeft];
这段代码:左边间距 = self.view左边间距 x 1.0 + margain
一个NSLayoutConstraint对象就代表一个约束
创建约束对象的常用方法
+(id)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
view1 :要约束的控件
attr1 :约束的类型(做怎样的约束)
relation :与参照控件之间的关系
view2 :参照的控件
attr2 :约束的类型(做怎样的约束)
multiplier :乘数
c :常量
自动布局有个核心公式
obj1.property1 =(obj2.property2 * multiplier)+ constant value