Skip to content

Instantly share code, notes, and snippets.

@nathanborror
Last active August 29, 2015 14:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nathanborror/7fc6a6b404b6c20d5c2e to your computer and use it in GitHub Desktop.
Save nathanborror/7fc6a6b404b6c20d5c2e to your computer and use it in GitHub Desktop.

Auto-Layout

Very simple convenience class to make working with Auto-Layout easier.

class Layout {

    let views: [String:UIView]
    let superview: UIView

    init(views: [String:UIView], superview: UIView) {
        self.views = views
        self.superview = superview
    }

    func addConstraints(constraints:[String]) {
        for constraint in constraints {
            let layout = NSLayoutConstraint.constraintsWithVisualFormat(constraint, options: NSLayoutFormatOptions(0), metrics: nil, views: self.views)
            self.superview.addConstraints(layout)
        }
    }

}

Usage:

let layout = Layout(views: ["table": tableView, "input": inputView], superview: self.view)

layout.addConstraints([
  "H:|[table]|",
  "H:|[input]|",
  "V:|[table][input(44)]|",
])

Reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment