IOS NSLayoutConstraint 页面布局(通过代码添加约束)

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIView *blueView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    // 2.添加两个控件到父控件上
    // 2.1添加蓝色View
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
//    blueView.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:blueView];
    self.blueView = blueView;
    
    // 2.1添加红色View
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    
    // 1.禁用auturezing
#warning 注意, 设置父控件无效
//    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 3.添加约束
    // 3.1添加蓝色VIew距离父控件左边的距离固定为20  X
    
    /*
     Item == first item 需要设置约束的控件
     attribute == 需要设置的约束
     relatedBy == relation   等于
     toItem == second item    被参照的控件
     attribute == 需要设置的约束
     multiplier == multiplier  乘以
     constant = constant   加上
     */
   
    NSLayoutConstraint *leftCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
    [self.view addConstraint:leftCos];
    
    // 3.2添加蓝色VIew距离父控件右边的距离固定为20  宽度
    NSLayoutConstraint *rightCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    [self.view addConstraint:rightCos];
    
    // 3.3添加蓝色VIew距离父控件顶部边的距离固定为20  Y
    NSLayoutConstraint *topCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
    [self.view addConstraint:topCos];
    
    // 3.4添加蓝色View的高度 50  高
    NSLayoutConstraint *heightCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50];
    [blueView addConstraint:heightCos];
    
    
    // 4.设置红色约束
    // 红色的高度和蓝色高度一样  高度
    NSLayoutConstraint *redHeightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    [self.view addConstraint:redHeightCos];
    
    // 红色的右边和蓝色的右边对齐  X
     NSLayoutConstraint *redRightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    [self.view addConstraint:redRightCos];
    
    //  红色的顶部和蓝色的底部距离固定  Y
     NSLayoutConstraint *redTopCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
    [self.view addConstraint:redTopCos];
    
    // 红色的宽度等于蓝色宽度的一半  宽度
   NSLayoutConstraint *redwidthCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
    [self.view addConstraint:redwidthCos];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%@", NSStringFromCGRect(self.blueView.frame));
}

@end

 

posted on 2017-05-22 16:20  守望星空  阅读(326)  评论(0编辑  收藏  举报

导航