EasyAnchor is a tiny UIView extension that reduces Auto Layout boilerplate with fluent, chainable helpers.
Auto Layout requires translatesAutoresizingMaskIntoConstraints = false on each view. layout { ... } handles that for you.
Make sure the view is inside a superview before applying constraints (the helpers unwrap superview).
button.layout {
view.addSubview($0)
$0.width(100)
.height(50)
.leading()
.bottom(constraint: redView.bottomAnchor, 0)
}or with size:
button.layout {
view.addSubview($0)
$0.size(equalTo: 50)
.leading()
.bottom(constraint: redView.bottomAnchor, 0)
}For trailing and bottom, the helpers invert the constant for readability:
button.layout {
view.addSubview($0)
$0.trailing(16) // Offset 16 from trailing.
.bottom(16) // Offset 16 from bottom.
}More helpers:
button.layout {
view.addSubview($0)
$0.top().leading().trailing().bottom()
}button.layout {
view.addSubview($0)
$0.fill()
}button.layout {
view.addSubview($0)
$0.fill(insets: UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16))
}button.layout {
view.addSubview($0)
$0.center()
}button.layout {
view.addSubview($0)
$0.centerY()
}button.layout {
view.addSubview($0)
$0.centerY(50)
}button.layout {
view.addSubview($0)
$0.centerY(constraint: redView.centerYAnchor, 10)
}button.layout {
view.addSubview($0)
$0.centerX()
}button.layout {
view.addSubview($0)
$0.centerX(50)
}button.layout {
view.addSubview($0)
$0.centerX(constraint: redView.centerXAnchor, 10)
}Some helpers accept priorities or constraint types:
button.layout {
view.addSubview($0)
$0.width(200, priority: .defaultHigh)
.height(.lessThanOrEqual, 44)
.top(priority: .greaterThanOrEqual)
}A class based building block enables shorthand setup right after initialization. It's common to create a view and configure it with a closure like this:
let okButton: UIButton = {
let button = UIButton()
button.backgroundColor = UIColor.blue
button.setTitleColor(UIColor.white, for: .normal)
button.setTitle("OK", for: .normal)
button.layer.cornerRadius = 8
return button
}()Here is shorthand syntax for above code.
let okButton = UIButton().config {
$0.backgroundColor = .blue
$0.setTitleColor(.white, for: .normal)
$0.setTitle("OK", for: .normal)
$0.layer.cornerRadius = 8
}decorate { ... } is available for legacy code, but prefer config { ... }.
Enable or disable Auto Layout on a specific view:
button.useAutoLayout = true // enable
button.useAutoLayout = false // disableview.removeSubviews()avatarView.squircle(12)Copy EasyAnchor.swift and Config.swift to your project, or
pod 'EasyAnchor', :git => 'https://github.com/PhanithNY/EasyAnchor.git'
From Xcode menu bar:
- File
- Swift Packages
- Add Package Dependency...
- Paste the repo url
https://github.com/PhanithNY/EasyAnchor.git
PhanithNY, phanith.ny@icloud.com
EasyAnchor is available under the MIT license. See the LICENSE file for more info.