Latest 4.2.4
Homepage https://github.com/wlgemini/Driftwood
License MIT
Platforms ios 8.0
Frameworks UIKit
Authors

Version
License
Platform

A lightweight, pure-Swift library for AutoLayout.

Requirements

  • iOS 8.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()
box.translatesAutoresizingMaskIntoConstraints = false
superview.addSubview(box)
box.dw.make.left(20).top(20).right(-20).bottom(-20)

Or another way:

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

NOTE: ensuring set translatesAutoresizingMaskIntoConstraints to false on all appropriate views.

Attribute

All NSLayoutAttribute 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:

AttributeX property AttributeX func 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
AttributeY property AttributeY func 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
AttributeSize property AttributeSize func NSLayoutConstraint.Attribute
dw.width dw.make.width() .width
dw.height dw.make.height() .height

Relation & Multiplier & Priority

Relation & Priority are available in AttributeX func & AttributeY func:

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

Relation & Multiplier & Priority are available in AttributeSize func:

view.dw.make.width(100, by: .greaterThanOrEqual, multiply: 2, priority: .required)
  • Relation: default is .equal
  • Priority: default is .required
  • Multiplier: default is 1

dw.update

You can use the method 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 the method dw.remove to removing any existing constraints installed by Driftwood.

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

LayoutGuide

Driftwood can works with UILayoutGuide easily.

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

let box = UIView()
box.translatesAutoresizingMaskIntoConstraints = false
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 attaching a name to any View or LayoutGuide for debug print.

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

It will be print like this, if an error occurred:

Driftwood [dw.make] error: <UIView: 0x00007fc636525da0; Labeled: 'MyView'> already have 'left' constraint.

NOTE: In release, Driftwood will not print any debug info.

Demo

You can download this repo to see more usage.

Author

wlgemini, [email protected]

License

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

Latest podspec

{
    "name": "Driftwood",
    "version": "4.2.4",
    "summary": "A lightweight, pure-Swift library for AutoLayout.",
    "homepage": "https://github.com/wlgemini/Driftwood",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "wlgemini": "[email protected]"
    },
    "source": {
        "git": "https://github.com/wlgemini/Driftwood.git",
        "tag": "4.2.4"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Driftwood/Classes/**/*",
    "swift_version": "4.2",
    "frameworks": "UIKit"
}

Pin It on Pinterest

Share This