QuickAnchors

QuickAnchors is a library that helps iOS developers write UIKit layout code more easily. With QuickAnchors you are able to add subviews and set up constraints in a single line of code, with a compact syntax that is easy to write and clear to read.

I wrote QuickAnchors because I was tired of all the boilerplate that’s required with programmatic UIKit layouts. At this point, I’ve used QuickAnchors in multiple projects and feel comfortable making it available for public use.

With QuickAnchors, instead of writing code that looks like this:

someView.addSubview(anotherView)
NSLayoutConstraint.activate([
    anotherView.topAnchor.constraint(equalTo: someView.topAnchor, constant: 50),
    anotherView.leadingAnchor.constraint(equalTo: someView.leadingAnchor, constant: 20),
    anotherView.heightAnchor.constraint(equalToConstant: 100)
])

You instead write something like this:

someView.quickAdd(anotherView, [ 
    topToTop(50), 
    leadingToLeading(20), 
    heightToConstant(100) 
])

Or this:

someView.quickAdd(anotherView, [ 50, 20, nil, nil ], [ heightToConstant(100) ])

Behind the scenes, the quickAdd function creates normal programmatic NSLayoutConstraints. This means that layouts can easily be expressed with any combination of QuickAnchors and normal old NSLayoutConstraint objects.

I’ve never released a package or library meant for public use before, but you can read all about the different ways you can use QuickAnchors on the repo’s readme. I’m also planning on writing some example blog posts and sample projects for QuickAnchors in the coming weeks and months.