AutoLayout
——不同级的控件的相互约束是添加在高层级上。 ——同级别的控件的相互约束是添加在它们的父控件上。 ——不同分支控件的相互约束是添加在它们向上追溯到的第一个共同父控件。 这3条规则在代码创建时有用。利用storyboard时系统自动帮我们添加好了。 (2)用代码实现AutoLayout。步骤就是先创建布局约束对象,然后把这个对象添加到需要约束的控件中。 ——需要先禁止需要约束的控件的autoresizing功能。 ——添加约束对象之前,得保证该控件已经添加到各自的父控件中。 01.- (void)viewDidLoad { 02.[super viewDidLoad]; 03.//保证需要约束的控件已有父控件 04.UIView *blueView=[[UIView alloc]init]; 05.blueView.backgroundColor=[UIColor blueColor]; 06.[self.view addSubview:blueView]; 07. 08.//关闭需要约束的控件的autoresizing 09.blueView.translatesAutoresizingMaskIntoConstraints=NO; 10. 11.//定义约束对象,并添加(注意添加到哪里),宽高100,居中 12.NSLayoutConstraint *width=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]; 13.[blueView addConstraint:width]; 14. 15.NSLayoutConstraint *height=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]; 16.[blueView addConstraint:height]; 17. 18.NSLayoutConstraint *centerX=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; 19.[self.view addConstraint:centerX]; 20. 21.NSLayoutConstraint *centerY=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]; 22.[self.view addConstraint:centerY]; 23.}
01.- (void)viewDidLoad { 02.[super viewDidLoad]; 03.//保证需要约束的控件已有父控件 04.UIView *blueView=[[UIView alloc]init]; 05.blueView.backgroundColor=[UIColor blueColor]; 06.[self.view addSubview:blueView]; 07. 08.UIView *redView=[[UIView alloc]init]; 09.redView.backgroundColor=[UIColor redColor]; 10.[self.view addSubview:redView]; 11. 12.//关闭需要约束的控件的autoresizing 13.blueView.translatesAutoresizingMaskIntoConstraints=NO; 14.redView.translatesAutoresizingMaskIntoConstraints=NO; 15. 16.//先调整blueView 17.NSLayoutConstraint *height=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40]; 18.[blueView addConstraint:height]; 19. 20.NSLayoutConstraint *left=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20]; 21.[self.view addConstraint:left]; 22.//注意-20 23.NSLayoutConstraint *right=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20]; 24.[self.view addConstraint:right]; 25. 26.NSLayoutConstraint *top=[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20]; 27.[self.view addConstraint:top]; 28. 29.//调整redView 30.NSLayoutConstraint *heightR=[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]; 31.[self.view addConstraint:heightR]; 32. 33.NSLayoutConstraint *widthR=[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0]; 34.[self.view addConstraint:widthR]; 35. 36.NSLayoutConstraint *rightR=[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]; 37.[self.view addConstraint:rightR]; 38.//注意redView的top是相对于blueView的bottom定位的 39.NSLayoutConstraint *topR=[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20]; 40.[self.view addConstraint:topR]; 41.}
——控件需要用 [ ] 括起来 —— | 表示父控件的边界 ——乘除法则使用不便,部分还需要使用constraintWithItem:辅助
01.- (void)viewDidLoad { 02.[super viewDidLoad]; 03.//保证需要约束的控件已有父控件 04.UIView *blueView=[[UIView alloc]init]; 05.blueView.backgroundColor=[UIColor blueColor]; 06.[self.view addSubview:blueView]; 07. 08.UIView *redView=[[UIView alloc]init]; 09.redView.backgroundColor=[UIColor redColor]; 10.[self.view addSubview:redView]; 11. 12.//关闭需要约束的控件的autoresizing 13.blueView.translatesAutoresizingMaskIntoConstraints=NO; 14.redView.translatesAutoresizingMaskIntoConstraints=NO; 15. 16.//利用VFL语言,但是有的不能实现,比如宽度的倍数,因为不能用乘除 17.NSArray *cons0=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView]-20-|" options:0 metrics:nil views:@{@"blueView":blueView}]; 18.[self.view addConstraints:cons0]; 19. 20.NSArray *cons1=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[blueView(40)]-20-[redView(==blueView)]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView":blueView,@"redView":redView}]; 21.[self.view addConstraints:cons1]; 22. 23.NSArray *cons2=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(==blueView)]" options:0 metrics:nil views:@{@"blueView":blueView,@"redView":redView}]; 24.[self.view addConstraints:cons2]; 25.}
——其中metrics一个存放占位符号和值得字典。比如如下,当然,我们可以另写一个NSDictionary字典来存放: 1.NSArray *cons0=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[blueView]-margin-|" options:0 metrics:@{@"margin":@20} views:@{@"blueView":blueView}];
1.NSArray *cons0=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[blueView]-margin-|" options:0 metrics:@{@"margin":@20} views:NSDictionaryOfVariableBindings(blueView,redView)];
(6)AutoLayout也可以做动画,即改变约束的值。 ——动画效果还需要UIView的animationWithDuration配合。需要注意的是,此处需要先修改好约束值,然后在动画方法中调用布局方法即layoutIfNeeded。 ——其中只有常量constant可以修改,其他的基本都是readonly只读的,不能修改。 01.#import "ViewController.h" 02. 03.@interface ViewController () 04.@property(nonatomic,strong) NSLayoutConstraint *con3; 05.@end 06. 07.@implementation ViewController 08. 09.- (void)viewDidLoad { 10.[super viewDidLoad]; 11...... 12.NSLayoutConstraint *con3=[NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0]; 13.[self.view addConstraint:con3]; 14.self.con3=con3; 15.} 16. 17.-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 18.self.con3.constant=100; 19.[UIView animateWithDuration:1.0 animations:^{ 20.[self.view layoutIfNeeded]; 21.}]; 22.} |