VFL约束

一般约束

NSLayoutConstraint(item: , attribute: , relatedBy: , toItem: , attribute: , multiplier: , constant: )
* 约束 NSLayoutConstraint  参数 说明:
* item 自身控件
* attribute  自身控件的属性
* relatedBy 大小判断 如:大于等于 小于等于 等于
* toItem 相对控件
* attribute 相对控件的属性
* multiplier 属性值乘多少
* constant : 属性值加多少
* NSLayoutConstraint : ( item:某个控件  attribute:属性值  relatedBy:等于  toItem:相对控件  attribute:属性值  multiplier:属性值乘多少  constant:属性值价多少)
*
* 添加约束 addConstraint

VFL约束(Visual Format Language)

NSLayoutConstraint.constraints(withVisualFormat: , options: , metrics: <[String : Any]?>, views: <[String : Any]>)

**
* withVisualFormat:VFl语句
* options:对齐方式
* metrics:传入一个字典 ,VFL中的key对应的值
* views:传入一个字典,VFL中key对应的View
**

//举个例子
let buttonConstraintV = NSLayoutConstraint.constraints(withVisualFormat: "V:|-100-[button(30)]", options: .directionMask, metrics: nil, views: ["button":alertTestButton])
alertTestButton.superview?.addConstraints(buttonConstraintV)

let buttonConstraintH = NSLayoutConstraint.constraints(withVisualFormat: "H:|-100-[button]-100-|", options: .directionMask, metrics: nil, views: ["button":alertTestButton])
alertTestButton.superview?.addConstraints(buttonConstraintH)

let buttonConstraintH = NSLayoutConstraint.constraints(withVisualFormat: "H:|-m-[button]-m-|", options: .directionMask, metrics: ["m":100], views: ["button":alertTestButton])
alertTestButton.superview?.addConstraints(buttonConstraintH)

V:代表垂直方向
H:代表水平方向
|: 表示父View的边界
-: 表示间隔

|-10-[button] :表示button距离边界为10
V:|-100-[button(30)] :表示垂直方向上button距离上方边界的距离为100 , button的高度为30
V:|-100-[button(text)] :表示垂直方向上button距离上方边界的距离为100 , button的高度 与text等高
H:|-100-[button]-100-|:表示水平方向上button距离左边界100,距离右边界100
H:|-m-[button]-m-|:表示水平方向上button距离左边界m,距离右边界m(可以用来动态改变值)
H:[button1][button2]:表示水平方向上button1后面紧跟button2
H:[button1]-10-[button2]:表示水平方向上button1距离为10后面跟着button2
m值以[key:value]字典的方式设为metrics参数(数值)
button值[key:value]字典的方式设为views参数(view

你可能感兴趣的:(VFL约束)