Driftwood 5.3.4

Driftwood 5.3.4

wlgemini 维护。



Driftwood 5.3.4

  • 作者:
  • wlgemini

Driftwood

Driftwood CI Version License Platform

英文|中文

Driftwood 是一个用于使 iOS、tvOS 和 macOS 上的 Auto Layout 简化的 DSL。

需求

  • iOS 8.0+/macOS 10.11+/tvOS 9.0+
  • Swift 4.2+

安装

Driftwood 通过 CocoaPods 提供。要安装它,只需将以下行添加到你的 Podfile 中

pod 'Driftwood'

Driftwood 使用简单,你只需几行代码就可以实现完整的约束满足。

例如,我们想要布局一个监听其 superview 边缘并具有 0pt 填充的框。

let box = UIView()
superview.addSubview(box)
box.dw.make().left(0).top(0).right(0).bottom(0)

或者另一种方法

let box = UIView()
superview.addSubview(box)
box.dw.make().edge(insets: .zero)

属性

Driftwood 有所有 NSLayoutConstraint.Attribute 情况。

例如,假设 view1view2 的底部,偏移了 10pt。

view1.dw.make().top(10, to: view2.dw.bottom)

NSLayoutConstraint.Attribute 的完整列表

水平属性属性 水平属性函数 NSLayoutConstraint.Attribute
dw.left dw.make().left() .left
dw.right dw.make().right() .right
dw.leading dw.make().leading() .leading
dw.trailing dw.make().trailing() .trailing
dw.centerX dw.make().centerX() .centerX
dw.leftMargin dw.make().leftMargin() .leftMargin
dw.rightMargin dw.make().rightMargin() .rightMargin
dw.leadingMargin dw.make().leadingMargin() .leadingMargin
dw.trailingMargin dw.make().trailingMargin() .trailingMargin
垂直属性属性 垂直属性函数 NSLayoutConstraint.Attribute
dw.top dw.make().top() .top
dw.bottom dw.make().bottom() .bottom
dw.centerY dw.make().centerY() .centerY
dw.lastBaseline dw.make().lastBaseline() .lastBaseline
dw.firstBaseline dw.make().firstBaseline() .firstBaseline
dw.topMargin dw.make().topMargin() .topMargin
dw.bottomMargin dw.make().bottomMargin() .bottomMargin
dw.centerYWithinMargins dw.make().centerYWithinMargins() .centerYWithinMargins
尺寸属性属性 尺寸属性函数 NSLayoutConstraint.Attribute
dw.width dw.make().width() .width
dw.height dw.make().height() .height

关系 & 相乘 & 优先级

  • relation: 默认值是 .equal
  • multiply: 默认值是 1
  • priority: 默认值是 .required

dw.make()

如上图所示,您可以使用dw.make()轻松创建完整约束。

dw.update()

您可以使用 dw.update() 更新约束的 constantpriority 值。

view1.dw.update().top(200)

view2.dw.update().left(100, priority: .required)

dw.remake()

dw.remake()dw.make() 类似,但首先会移除 Driftwood 安装的现有约束。

view.dw.remake().left(20).top(30).width(20).height(10)

dw.remove()

您可以使用 dw.remove() 移除 Driftwood 安装的任何现有约束。

view.dw.remove().left().top()

dw.removeAll()

有时,你只想删除之前安装的所有约束。你可以使用 dw.removeAll() 来删除 Driftwood 安装的所有现有约束。

view.dw.removeAll()

LayoutGuide

Driftwood 可以轻松与 LayoutGuide 一起使用。

let guide = UILayoutGuide()
superview.addLayoutGuide(guide)
guide.dw.make().left(10).top(10).height(10).width(10)

let box = UIView()
superview.addSubview(box)
box.dw.make().top(0, to: guide.dw.bottom).left(0).right(0).height(10)

Cache

Driftwood 安装的所有约束都将被缓存以供将来重用。

Debug

你可以为任何 ViewLayoutGuide 添加名称以供调试。

view.dw.make(labeled: "MyView").left(0).left(0)

日志将如下所示

<Driftwood.@ViewController#23.[make left].(UIView`MyView`:0x00007fc636525da0)>: Duplicated constraint.

如果出现“Unable to simultaneously satisfy constraints”,则每个由 Driftwood 安装的约束的日志将如下所示

<Driftwood.@ViewController#23.[make left].(UIView`MyView`:0x00007fc636525da0.left == UIView:0x00007fc636525111.right)>

注意:在发布版中,Driftwood 不会记录调试信息。

Demo

你可以下载这个仓库以查看更多用法。

作者

wlgemini, [email protected]

许可证

Driftwood是在MIT许可下可用的。有关更多信息,请参阅LICENSE文件。