Skip to content

Driftwood is a DSL to make Auto Layout easy on iOS, tvOS and macOS.

License

Notifications You must be signed in to change notification settings

wlgemini/Driftwood

Repository files navigation

Driftwood CI Version License Platform

English|中文

Driftwood is a DSL to make Auto Layout easy on iOS, tvOS and macOS.

Usage

Driftwood is easy to use, you can make full constraints satisfication in just a few code.

Let's say we want to layout a box that is constrained to it’s superview’s edges with 0pts of padding.

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

Or another way:

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

Attribute

All NSLayoutConstraint.Attribute cases are available in Driftwood.

Let's say view1 is at the bottom of view2, offset with 10pts.

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

Full list of NSLayoutConstraint.Attribute:

Horizontal attribute property Horizontal attribute function 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
Vertical attribute property Vertical attribute function 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
Size attribute property Size attribute function NSLayoutConstraint.Attribute
dw.width dw.make().width() .width
dw.height dw.make().height() .height

Relation & Multiply & Priority

  • relation: default is .equal
  • multiply: default is 1
  • priority: default is .required

dw.make()

As you see above, you can use dw.make() to make full constraints easily.

dw.update()

You can use dw.update() to updating constant and priority value of a constraint.

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

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

dw.remake()

dw.remake() is similar to dw.make(), but will first remove all existing constraints installed by Driftwood.

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

dw.remove()

You can use dw.remove() to removing any existing constraints installed by Driftwood.

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

dw.removeAll()

Sometimes, you may just want to remove all constraints installed before. You can use dw.removeAll() to removing all existing constraints installed by Driftwood.

view.dw.removeAll()

LayoutGuide

Driftwood can works with LayoutGuide easily.

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

All constraints installed by Driftwood will be cached for future reuse.

Debug

You can labeled with a name to any View or LayoutGuide for debug.

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

It will be logs like this:

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

If resulting Unable to simultaneously satisfy constraints, it will be logs like this for each constraint installed by Driftwood:

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

NOTE: In release, Driftwood will not log debug info.

Demo

You can download this repo to see more usage.

Requirements

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

Installation

Driftwood is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Driftwood'

License

Driftwood is available under the MIT license. See the LICENSE file for more info.