- iOS 8 compatible
- Maximum readability
- No new concepts or magic
import Pin
adds new properties to Layoutable
elements such as views and layout guides.
Define a constraint using operators like so:
let constraint = label.bottomPin == view.bottomPin // NSLayoutConstraint
let widthConstraint = label.widthPin <= view.widthPin * 0.75 ~ UILayoutPriorityDefaultHigh
let topConstraint = label.topPin == imageView.bottomPin + 10
Pin a views center. offset
and multiplier
is optional.
titleLabel.pinCenter(to: view) // [NSLayoutConstraint]
Pin a views edges to another view. insets
is optional.
backgroundView.pinEdges(to: view) // [NSLayoutConstraint]
Pin a views edges to another views edge margins
longTextLabel.pinEdgeMargins(to: textContainer) // [NSLayoutConstraint]
Sequence
of NSLayoutConstraint
is extended when importing Pin with two methods.
let constraints = [
label.centerXPin == view.centerXPin,
label.width <= 250,
label.topPin == view.topLayoutGuide.bottomPin
]
constraints.activate() // Activates constraints
constraints.deactivate() // Deactivates constraints
You can create multiple constraints without storing them, for instance, in viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
var constraints = [NSLayoutConstraint]()
constraints += backgroundView.pinEdges(to: view)
constraints += [
titleLabel.topPin == topLayoutGuide.bottomPin + 30,
titleLabel.centerXPin == view.centerXPin,
titleLabel.widthPin <= view.widthPin * 0.75,
textContainer.bottomPin == bottomLayoutGuide.topPin - 30,
textContainer.leftPin == view.leftMarginPin,
textContainer.rightPin == view.rightMarginPin
]
constraints += longTextLabel.pinEdgeMargins(to: textContainer)
constraints.activate()
}
You can also use NSLayoutConstraint.make
for an even cleaner implementation:
NSLayoutConstraint.make { constraints in
constraints += backgroundView.pinEdges(to: view)
constraints.append(textLabel.width == 200)
}.activate()
If you don't fancy operators, you can use the more expressive syntax, or mix as you like:
NSLayoutConstraint.activate([
label.topPin.equal(to: view.topMarginPin),
label.centerXPin.equal(to: view.centerXPin),
label.leftPin.greaterThanOrEqual(to: view.leftPin * 0.25) ~ UILayoutPriorityDefaultHigh,
label.rightPin.lessThanOrEqual(to: view.rightPin.multiplied(by: 0.75).offset(by: 10)).prioritized(at: UILayoutPriorityDefaultHigh)
])
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Pin into your Xcode project using Carthage, add the following to your Cartfile
:
github "formbound/Pin"
- David Ask (@davidask)
Pin is released under the MIT license. See LICENSE for details.
Formbound AB is a software development firm devoted to open-source development.