Small Auto Layout helpers for UIKit. Describe constraints as short key-path expressions, and add a subview with its constraints in one call. translatesAutoresizingMaskIntoConstraints is handled for you.
CocoaPods:
pod 'GLayout', '~> 1.0'Swift Package Manager isn't supported yet. Requires iOS 13+ and Swift 5.
import GLayout
// Fill the parent with insets.
view.addSubview(card, constraints: Constraints.with(insets: UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)))
// Center with a fixed size.
view.addSubview(avatar,
constraints: Constraints.center,
singleConstraints: Constraints.fixedSize(size: CGSize(width: 64, height: 64)))
// Individual constraints; the second view defaults to the parent.
view.addSubview(button, constraints: [
equal(\.leadingAnchor, constant: 20),
equal(\.trailingAnchor, constant: -20),
equal(\.bottomAnchor, \.safeAreaLayoutGuide.bottomAnchor, constant: -16)
])
// Relative to a sibling.
view.addSubview(title, constraints: [
equal(\.topAnchor, to: avatar, \.bottomAnchor, constant: 8),
equal(\.centerXAnchor),
lessOrEqual(\.widthAnchor, constant: -40)
])| Function | Makes |
|---|---|
equal, greaterOrEqual, lessOrEqual |
A constraint between the view and its parent, or another view via to:. Pass a second key path to pin to a different anchor. Options: constant, priority, identifier, and multiplier for dimensions |
equal(\.heightAnchor, constant: 44) |
A constant size (SingleConstraint) |
| Preset | Pins |
|---|---|
Constraints.pin / .with(insets:) |
All four edges |
Constraints.with(top:bottom:left:right:) |
Only the edges you pass |
Constraints.center |
Center X and Y |
Constraints.fixedSize(size:) |
Width and height |
RespectSafeAreaConstraints.pin / .with(insets:) |
Edges, top and bottom to the safe area |
RespectBottomSafeAreaConstraints.pin / .with(insets:) |
Edges, bottom to the safe area |
spinner.setConstraints(Constraints.fixedSize(size: CGSize(width: 24, height: 24)))
badge.setConstraints(to: icon, constraints: [equal(\.topAnchor), equal(\.trailingAnchor)])button.setConstraints([equal(\.heightAnchor, identifier: "height", constant: 44)])
button.constraints["height"]?.constant = 56MIT. See LICENSE.