Pintastic is a Swift Package for iOS that enables developers to layout views quickly and easily.
- Method chaining makes Pintastic fun and easy to use. Save a lot of typing and start getting stuff done!
- Because Pintastic is lightweight it is also low risk.
let container = UIView()
container.translatesAutoresizingMaskIntoConstraints = false
container.backgroundColor = .orange
let nested = UIView()
nested.translatesAutoresizingMaskIntoConstraints = false
nested.backgroundColor = .green
view.addSubview(container)
container.addSubview(nested)
container
.pin(to: view.safeAreaLayoutGuide)
.pinEdges()
.activate()
nested
.pin()
.pinWidthEqualToHeight()
.activate()
nested
.pin(to: container)
.pinCenters()
.pinWidths(multiplier: 0.5)
.activate()
- Easy to use wrapper around autolayout
- Supports most common layout scenarios
- Integrates seamlessly with
NSLayoutConstraint
andNSLayoutAnchor
when more intricate layouts are required - Method chaining means fewer constants and variables
- Supports
UIView
andUILayoutGuide
- Lightweight
Pintastic comes as a Swift Package and can be installed using the Swift Package Manager.
From Xcode go to File -> Add Packages. Copy and paste the following into the search box:
https://github.com/robertcrabtree/Pintastic.git
Here's how you would constrain the edges of a view.
subview
.makePin(to: container)
.pinEdges()
.activate()
subview
.makePin(to: container)
.pinLeadingEdges(constant: 10)
.pinTrailingEdges(constant: -10)
.activate()
Here's how you would do some basic sizing.
subview
.makePin()
.pin(width: 100.0) // constant width
.pin(height: 100.0) // constant height
.activate()
subview
.makePin(to: container)
.pinWidths(multiplier: 0.5) // width relative to another
.pinHeights(multiplier: 0.5) // height relative to another
.activate()
Here's how you would position a view relative to another.
leftView
.makePin(to: centerView)
.pinBefore(constant: -20)
.activate()
topView
.makePin(to: centerView)
.pinAbove(constant: -20)
.activate()
Here's how views are centered.
centerView
.makePin(to: container)
.pinCenters()
.activate()
centerView
.makePin(to: container)
.pinHorizontalCenters()
.pinVerticalCenters()
.activate()
If you need a finer granularity of control you can add custom constraints.
let constraintID = "left.width.equal.to.right.height"
leftView
.makePin(to: rightView)
.addConstraint(withIdentifier: constraintID) {
leftView.widthAnchor.constraint(equalTo: rightView.heightAnchor)
}
.constraint(withIdentifier: constraintID, handler: { constraint in
constraint?.priority = .defaultLow
})
.activate()
Sometimes you need to alter constraint values beyond the time of initialization.
let pin = subview
.makePin()
.pinWidth(constant: 100)
.pinHeight(constant: 100)
.activate()
// ...
if let widthConstraint = pin.constraint(ofType: .width) {
widthConstraint.constant *= 2.0
}
if let heightConstraint = pin.constraint(ofType: .height) {
heightConstraint.constant *= 2.0
}
Pintastic is published under the Apache 2.0 license.