yavfl 是一个用于 Auto Layout 的可视化格式语言。
在 yavfl 中,您可以像以下这样编写可视化格式语言的代码
visualFormat(label, textField) { l, t in
.V ~ |-40-[l,==32]
.V ~ |-40-[t,==32]
.H ~ |-20-[l,==t]-10-[t]-|
}
原始可视化格式的等效代码如下
let dic = ["l": label, "t": textField]
label.setTranslatesAutoresizingMaskIntoConstraints(false)
textField.setTranslatesAutoresizingMaskIntoConstraints(false)
let c1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[l(32)]", options: nil, metrics: nil, views: dic)
let c2 = NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[t(32)]", options: nil, metrics: nil, views: dic)
let c3 = NSLayoutConstraint.constraintsWithVisualFormat("|-20-[l(==t)]-10-[t]-|", options: nil, metrics: nil, views: dic)
self.view.addConstraints(c1)
self.view.addConstraints(c2)
self.view.addConstraints(c3)
yavfl | 原始 |
---|---|
.H ~ [button]-[textField] |
[button]-[textField] |
.H ~ [button,>=50] |
[button(>=50)] |
.H ~ |-50-[purpleBox]-50-| |
|-50-[purpleBox]-50-| |
.V ~ [topField]-10-[bottomField] |
V:[topField]-10-[bottomField] |
.H ~ [maroonView]-0-[blueView] |
[maroonView][blueView] |
.H ~ [button,==100~20] |
[button(100@20)] |
.H ~ [button1,==button2] |
[button1(==button2)] |
.H ~ [flexibleButton,>=70,<=100] |
[flexibleButton(>=70,<=100)] |
.H ~ |-[find]-[findNext]-[findField,>=20]-| |
|-[find]-[findNext]-[findField(>=20)]-| |
visualFormat
最多支持 5 个视图。由于 Swift 的语法限制,一些语法部分已更改。
()
作为视图的 predicate。[button1,==button2]
而不是 [button1(==button2)]
。~
而不是 :
作为 orientation,因为它们不能用于自定义运算符。.H ~ |-[view]-|
而不是 H:|-[view]-|
。[view,==100~200]
而不是 [view(==100@200)]
。.H
或 .V
) 和运算符 ~
。~
是创建和应用约束的触发器。-
)。[v1][v2]
。请使用 [v1]-0-[v2]
。==
、<=
或 >=
)。[view,100]
。请使用 [view,==100]
。pod 'yavfl'
或者,只需将 yavfl.swift
拷贝到您的项目中。