/KoalaTeaAutoLayout

A custom AutoLayout DSL wrapper for iOS

Primary LanguageSwiftMIT LicenseMIT

KoalaTeaAutoLayout

CI Status Version License Platform

A custom AutoLayout DSL wrapper building on top of the amazing work of John Sundell in the blog post Building DSLs in Swift.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

iOS 11.0+

Installation

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

pod 'KoalaTeaAutoLayout'

Usage

Always have to import first

import KoalaTeaAutoLayout

And then you can use the library like so:

let layoutView = UIView()
self.view.addSubview(layoutView)
layoutView.backgroundColor = .red
layoutView.layout {
    $0.top == self.view.safeAreaLayoutGuide.topAnchor
    $0.leading == self.view.leadingAnchor + 20
    $0.trailing == self.view.trailingAnchor - 20
    $0.height.equal(to: self.view.heightAnchor, multiplier: 0.1)
}

or you could have the constraints returned and activate/deactivate them at will:

var constraints1: [NSLayoutConstraint] = []
var constraints2: [NSLayoutConstraint] = []

self.view.addSubview(layoutView)
layoutView.backgroundColor = .blue
constraints1 = layoutView.returnedLayout {
    return [
        $0.centerXAnchor == self.view.centerXAnchor,
        $0.bottom == self.view.safeAreaLayoutGuide.bottomAnchor,
        $0.height == 80,
        $0.width == $0.height + 100,
    ]
}

constraints2 = layoutView.returnedLayout {
    return [
        $0.top == layoutView2.bottomAnchor,
        $0.centerXAnchor == self.view.centerXAnchor,
        $0.height == 80,
        $0.width == $0.height + 100,
    ]
}
constraints2.deactivateAll()

animate()

func animate() {
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
        self.constraints1.deactivateAll()
        self.constraints2.activateAll()

        UIView.animate(withDuration: 0.5, animations: {
            self.view.layoutIfNeeded()
        }, completion: { _ in
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                self.constraints2.deactivateAll()
                self.constraints1.activateAll()

                UIView.animate(withDuration: 0.5, animations: {
                    self.view.layoutIfNeeded()
                }, completion: { _ in
                    self.animate()
                })
            }
        })
    }
}

There are also some convenience functions setup for common use cases:

let edgesToSuperview = UIView()
edgesToSuperview.constrainEdgesToSuperview()

let centerToSuperview = UIView()
centerToSuperview.constrainCenterToSuperview()

let edgesToAnotherView = UIView()
edgesToAnotherView.constrainEdges(to: centerToSuperview)

Contributing

Open an issue.

Open a PR.

or shoot me an email and I'll get back to you.

Author

Craig Holliday

email: hello@craigholliday.net

twitter: https://twitter.com/TheMrHolliday

License

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