pragma mark 1
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
view1.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view1];
UIView* view2 = [[UIView alloc] initWithFrame:CGRectMake(100,200 , 33, 33)];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
// 这句话别忘了 要去掉系统的constraint
view2.translatesAutoresizingMaskIntoConstraints = NO;
// y对齐
NSLayoutConstraint *centerYConstrait = [NSLayoutConstraint constraintWithItem:view2
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:view1
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0];
// view1的右边 -10-view2的左边
NSLayoutConstraint *horizontalSpaceConstrait = [NSLayoutConstraint constraintWithItem:view2
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:view1
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:10];
// 高度
NSLayoutConstraint *heightConstrait = [NSLayoutConstraint constraintWithItem:view2
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:view1
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0];
// 宽度
NSLayoutConstraint *widthConstrait = [NSLayoutConstraint constraintWithItem:view2
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:view1
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0];
[self.view addConstraints:@[centerYConstrait, horizontalSpaceConstrait, widthConstrait,heightConstrait]];
pragma 2
//////
// UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
// view1.backgroundColor = [UIColor redColor];
// [self.view addSubview:view1];
//
//
// UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(100,200 , 33, 33)];
// view2.backgroundColor = [UIColor orangeColor];
// [self.view addSubview:view2];
// view2.translatesAutoresizingMaskIntoConstraints = NO;
//
//// // 宽度 高度
// NSLayoutConstraint *heightConstrait = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
// // 宽度
// NSLayoutConstraint *widthConstrait = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual
// toItem:view1 attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
//
// NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
// NSLayoutConstraint *constraintBottom = [NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeBottom multiplier:1 constant:40];
//
// [self.view addConstraints:@[heightConstrait,widthConstrait]];
// [self.view addConstraint:constraintTop];
// [self.view addConstraint:constraintBottom];
// item1 =(>=,<=) multiplier * item2 + constant。
pragma 3
// UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 120, 50)];
// UIView *view2 = [[UIView alloc] init];
// [self.view addSubview:view1];
// [self.view addSubview:view2];
// view1.translatesAutoresizingMaskIntoConstraints = NO;
// view2.translatesAutoresizingMaskIntoConstraints = NO;
// view1.backgroundColor = [UIColor blueColor];
// view2.backgroundColor = [UIColor grayColor];
//
// //set view1 height and width
// [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];
// [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];
// //set view2 height and width
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];
// //set relationship between view1 and view2
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeRight multiplier:1 constant:100]];
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
// //set relationship between topView and view1
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:20]];
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
// 问题
1、translatesAutoresizingMaskIntoConstraints 开关与约束
2、添加约束的对象???