#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//使用约束设置视图的位置和大小 frame是没有意义的 或者说约束要比frame优先级高
UIView *redView = [[UIView alloc]initWithFrame:CGRectZero];
redView.backgroundColor = [UIColor redColor];
//translatesAutoresizingMaskIntoConstraints 默认是YES 为了不影响停靠模式对约束的设置,需要将这个属性设置成NO
redView.translatesAutoresizingMaskIntoConstraints = NO;
//停靠模式
// redView.autoresizingMask
UIView *blueView = [[UIView alloc]init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView];
[self.view addSubview:blueView];
//创建约束 NSLayoutConstraint约束类
NSLayoutConstraint *redHConstraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-50];
//第一个参数:Item 要添加约束的视图/源视图
//第二个参数:attribute 源视图的属性 NSLayoutAttributeTrailing右侧
//第三个参数:relatedBy 和目标视图的关系/和参考视图的关系 NSLayoutRelationEqual等于
//第四个参数:toItem 参考视图
//第五个 参数:attribute 参考视图的属性
//第六个参数:multiplier 比例/倍数
//第七个参数:constant 约束值
NSLayoutConstraint *redVConstraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100];
//NSLayoutAttributeTop 上
NSLayoutConstraint *redWConatraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant:100];
//NSLayoutAttributeWidth 宽
NSLayoutConstraint *redtHConstraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:50];
//添加约束
[self.view addConstraint:redHConstraint];
[self.view addConstraint:redtHConstraint];
[self.view addConstraint:redWConatraint];
[self.view addConstraint:redVConstraint];
NSLayoutConstraint *blueHConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:-50];
//右 下 -- 约束值是负数
//左 上 -- 约束值是整数
NSLayoutConstraint *blueVConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeTop multiplier:1.0f constant:-10];
NSLayoutConstraint *blueWContstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:50];
NSLayoutConstraint *bluetHConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:20];
//添加约束 -- 添加一组约束
[self.view addConstraints:@[blueHConstraint,blueVConstraint,blueWContstraint,bluetHConstraint]];
}
@end
网友评论