iOS开发学习之路【UI界面】——Auto Layout
Auto Layout 简介
中文名:自动布局
使用 Interface Builder 添加约束
-
使用拖拽的方式添加约束 按住 Ctrl + 左键 拖拽
-
使用布局菜单添加约束
效果
代码实现约束
self.btn = [UIButton buttonWithType:UIButtonTypeSystem];
[self.btn setTitle:@"go go go ..." forState:UIControlStateNormal];
self.btn.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.btn];
// 绝对坐标,一般不使用
// self.btn.frame = CGRectMake(100, 200,300, 400);
// 相对布局
/*
NSLayoutRelation 关系
NSLayoutAttribute 属性
NSLayoutFormatOptions 格式选项
UILayoutPriority 优先级
*/
NSLayoutConstraint *cx = [NSLayoutConstraint constraintWithItem:self.btn attribute:(NSLayoutAttributeCenterX) relatedBy:(NSLayoutRelationEqual) toItem:(self.view) attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
NSLayoutConstraint *cy = [NSLayoutConstraint constraintWithItem:self.btn attribute:(NSLayoutAttributeCenterY) relatedBy:(NSLayoutRelationEqual) toItem:(self.view) attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
[self.view addConstraint:cx];
[self.view addConstraint:cy];
效果
Visual Format Language (VFL)
不推荐使用。