VFL的简单语法:
H : [ cancelButton (72)]-12-[ acceptButton (50)]
H : [ cancelButton (72)]-12-[ acceptButton (50)]
canelButton
宽
72
,
acceptButton
宽
50
,它们之间间距
12
H
:
[
wideView
(>=60@700)]
wideView
宽度大于等于
60point
,该约束条件优先级为
700
(优先级最大值为
1000
,优先级越高的约束越先被满足)
V
:[
redBox
]-[
yellowBox
(==
redBox
)]
竖直
方向上
,先
有
一个
redBox
,其下方紧接一个
高度
等于
redBox
高度
的
yellowBox
H
:|-
10-
[Find]-[
FindNext
]-[
FindField
(>=20)]-|
水平
方向上
,
Find
距离父
view
左边缘默认间隔宽度,之后是
FindNext
距离
Find
间隔默认宽度;再之后是宽度不小于
20
的
FindField
,它和
FindNext
以及父
view
右边缘的间距都是默认宽度。(
竖线“
|
”
表示superview的边缘)
eg:
// 注意: 在VFL语句中, 是不支持乘除法
// 代码方式实现自动布局VFL
// 创建一个Tableview;
UITableView *tableView = [[UITableViewalloc]init];
tableView.backgroundColor = [UIColorredColor];
#warning 代码实现自动布局,要设置下面的属性为NO tableView.translatesAutoresizingMaskIntoConstraints =NO;
[self.viewaddSubview:tableView];
// 创建输入框View
WCInputView *inputView = [WCInputViewinputView];
inputView.translatesAutoresizingMaskIntoConstraints =NO;
[self.viewaddSubview:inputView];
// 自动布局
// 水平方向的约束
NSDictionary *views = @{@"tableview":tableView,
@"inputView":inputView};
// 1.tabview水平方向的约束
NSArray *tabviewHConstraints = [NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-0-[tableview]-0-|"options:0metrics:nilviews:views];
[self.viewaddConstraints:tabviewHConstraints];
// 2.inputView水平方向的约束
NSArray *inputViewHConstraints = [NSLayoutConstraintconstraintsWithVisualFormat:@"H:|-0-[inputView]-0-|"options:0metrics:nilviews:views];
[self.viewaddConstraints:inputViewHConstraints];
// 注意: 在VFL语句中, 是不支持乘除法
// NSArray *redVeiwH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(==blueView / 2 )]" options:0 metrics:nil views:@{@"blueView" : blueView, @"redView": redView}];
// [self.view addConstraints:redVeiwH];
// 要结合下面的方法才能执行
NSLayoutConstraint *redVeiwW = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
[self.view addConstraint:redVeiwW];
具体参数如下:(storyBoard里面的约束)