Driftwood : A lightweight Swift library for AutoLayout

Jan 09, 2020 | Library

Requirements

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

Usage

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

pod 'Driftwood'

Quick Start

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 20pts of padding.

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

Or another way:

let box = UIView()
superview.addSubview(box)
box.dw.make().edge(insets: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))

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:

X-axis Attribute propertyX-axis Attribute functionNSLayoutConstraint.Attribute
dw.leftdw.make().left().left
dw.rightdw.make().right().right
dw.leadingdw.make().leading().leading
dw.trailingdw.make().trailing().trailing
dw.centerXdw.make().centerX().centerX
dw.leftMargindw.make().leftMargin().leftMargin
dw.rightMargindw.make().rightMargin().rightMargin
dw.leadingMargindw.make().leadingMargin().leadingMargin
dw.trailingMargindw.make().trailingMargin().trailingMargin
Y-axis Attribute propertyY-axis Attribute functionNSLayoutConstraint.Attribute
dw.topdw.make().top().top
dw.bottomdw.make().bottom().bottom
dw.centerYdw.make().centerY().centerY
dw.lastBaselinedw.make().lastBaseline().lastBaseline
dw.firstBaselinedw.make().firstBaseline().firstBaseline
dw.topMargindw.make().topMargin().topMargin
dw.bottomMargindw.make().bottomMargin().bottomMargin
dw.centerYWithinMarginsdw.make().centerYWithinMargins().centerYWithinMargins
Size Attribute propertySize Attribute functionNSLayoutConstraint.Attribute
dw.widthdw.make().width().width
dw.heightdw.make().height().height

Relation & Multiplier & Priority

Relation & Priority are available in X-axis Attribute function & Y-axis Attribute function:

view.dw.make().left(100, by: .greaterThanOrEqual, priority: .defaultLow)

Relation & Multiplier & Priority are available in Size Attribute function:

view.dw.make().width(100, by: .greaterThanOrEqual, multiply: 2, priority: .required)
  • Relation: default is .equal
  • Multiplier: 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()

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.swift#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.swift#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.

Author

wlgemini, wangluguang@live.com

#Layout

Language grammar tokenizer and theming/syntax highlighter with integrated editor

What should iOS developers learn SwiftUI, UIKit, or both?