autoLayout自动布局
三种方式添加约束:
1. 界面操作;
2. 手写代码;
3. 视觉化格式语言;
方式1
在属性框中输入相应的属性值即可(注意属性的冲突设置与属性缺失)
方式2:
//求算公式: redView.height = self.view.top*0.2 + 20
//给redView添加顶部间距约束,相对于self.view的顶部,设为20个像素
NSLayoutConstraint *redViewTop = [NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:20];
[self.view addConstraint:redViewTop];
方式3:
NSDictionary *dic = @{@"mainView":self.view,@"topView":redView,@"bottomView":blueView};
NSArray *arrBlueV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[topView]-20-[bottomView(==100)]" options:0 metrics:nil views:dic];
NSArray *arrBlueH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[topView]-20-[bottomView(==50)]" options:0 metrics:nil views:dic];
[self.view addConstraints:arrBlueV];
[self.view addConstraints:arrBlueH];