标签:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createViewWithConstraintItem]; [self createViewWithConstraint]; } - (void)createViewWithConstraintItem { UIView *redView = [[UIView alloc]init]; redView.backgroundColor=[UIColor redColor]; redView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:redView]; /**左边距50*/ NSLayoutConstraint *viewLeftConstraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1 constant:50.f]; /**上边距50*/ NSLayoutConstraint *viewTopConstraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100.f]; /*-------------------------------------------------------------------------------- 注意:通常在设置宽度高度的时候最好不要固定死,另外当viw1.attribute不等于view2.attribute*multiplier +constant的时候,我们则要constraintWithItem函数中的toItem设置为nil以及attribute参数设置 为NSLayoutAttributeNotAnAttribute ---------------------------------------------------------------------------------*/ /**宽度150*/ NSLayoutConstraint *viewWidthConstaint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150]; /**高度150*/ NSLayoutConstraint *viewHeightConstaint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150]; [self.view addConstraints:@[viewLeftConstraint,viewTopConstraint,viewWidthConstaint,viewHeightConstaint]]; } - (void)createViewWithConstraint { /**创建左边view*/ UIView *leftView = [[UIView alloc]init]; leftView.backgroundColor = [UIColor redColor]; leftView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:leftView]; /**创建右边view*/ UIView *rightView = [[UIView alloc]init]; rightView.backgroundColor = [UIColor blueColor]; rightView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:rightView]; /**创建右下方view*/ UIView *rightBottomView = [[UIView alloc]init]; rightBottomView.backgroundColor = [UIColor yellowColor]; rightBottomView.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:rightBottomView]; NSDictionary *viewDic = NSDictionaryOfVariableBindings(leftView,rightView,rightBottomView); /*距父视图左边距50以及自身宽度大于等于150*/ NSArray *leftView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[leftView(>=150)]" options:0 metrics:nil views:viewDic]; /*距父视图上边距100以及自身高度大于等于150*/ NSArray *leftView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[leftView(>=150)]" options:0 metrics:nil views:viewDic]; /*水平方向布局,rightView 在 leftView 右侧标准距离处,并且宽度不小于 50 点。*/ NSArray *rightView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[leftView]-[rightView(>=50)]" options:0 metrics:nil views:viewDic]; NSArray *rightView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[rightView(>=50)]" options:0 metrics:nil views:viewDic]; NSArray *rightBottomView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[leftView]-[rightBottomView(>=50)]" options:0 metrics:nil views:viewDic]; /*垂直方向布局距离父视图100,另外rightBottomView 在 rightView下方也就是相当于紧贴*/ NSArray *rightBottomView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[rightView(>=50)][rightBottomView(>=100)]" options:0 metrics:nil views:viewDic]; [self.view addConstraints:leftView_H]; [self.view addConstraints:leftView_V]; [self.view addConstraints:rightView_H]; [self.view addConstraints:rightView_V]; [self.view addConstraints:rightBottomView_H]; [self.view addConstraints:rightBottomView_V]; } @end
标签:
原文地址:http://www.cnblogs.com/thbbsky/p/4394834.html