一般約束
NSLayoutConstraint(item: <Any>, attribute: <NSLayoutAttribute>, relatedBy: <NSLayoutRelation>, toItem: <Any?>, attribute: <NSLayoutAttribute>, multiplier: <CGFloat>, constant: <CGFloat>)
* 約束 NSLayoutConstraint 參數(shù) 說明:
* item 自身控件
* attribute 自身控件的屬性
* relatedBy 大小判斷 如:大于等于 小于等于 等于
* toItem 相對控件
* attribute 相對控件的屬性
* multiplier 屬性值乘多少
* constant : 屬性值加多少
* NSLayoutConstraint : ( item:某個控件 attribute:屬性值 relatedBy:等于 toItem:相對控件 attribute:屬性值 multiplier:屬性值乘多少 constant:屬性值價多少)
*
* 添加約束 addConstraint
VFL約束(Visual Format Language)
NSLayoutConstraint.constraints(withVisualFormat: <String>, options: <NSLayoutFormatOptions>, metrics: <[String : Any]?>, views: <[String : Any]>)
**
* withVisualFormat:VFl語句
* options:對齊方式
* metrics:傳入一個字典 汗盘,VFL中的key對應(yīng)的值
* views:傳入一個字典疑苔,VFL中key對應(yīng)的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(可以用來動態(tài)改變值)
H:[button1][button2]:表示水平方向上button1后面緊跟button2
H:[button1]-10-[button2]:表示水平方向上button1距離為10后面跟著button2
m值以[key:value]字典的方式設(shè)為metrics參數(shù)(數(shù)值)
button值[key:value]字典的方式設(shè)為views參數(shù)(view