(1)
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
view1.backgroundColor = [UIColor redColor];
view1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view1];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
//
//
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];
(2)
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
view1.backgroundColor = [UIColor redColor];
view1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:view1];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
// [view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
//
//
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];
(3)
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 100, 100)];
view1.backgroundColor = [UIColor redColor];
view1.translatesAutoresizingMaskIntoConstraints = YES;
[self.view addSubview:view1];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
//
//
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];
translatesAutoresizingMaskIntoConstraints 约束与Autosizing控制。。。
当打开约束的时候,要约束条件完全,否则可能试图丢失。。。。。
网友评论
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:300]];
这个不应该是self.view addConstraint 么