PrettyLayout is a extra lightweight DSL to make Auto Layout easy on iOS. Using PrettyLayout, you can set up your Auto Layout constraints in simple way
In short, it allows you to replace this:
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let yellow = UIView()
yellow.backgroundColor = .yellow
yellow.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(yellow)
NSLayoutConstraint.activate([
yellow.leadingAnchor.constraint(equalTo: view.leadingAnchor),
yellow.trailingAnchor.constraint(equalTo: view.trailingAnchor),
yellow.topAnchor.constraint(equalTo: view.topAnchor),
yellow.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
let redBox = UIView()
redBox.backgroundColor = .red
redBox.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(redBox)
NSLayoutConstraint.activate([
redBox.widthAnchor.constraint(equalToConstant: 100),
redBox.heightAnchor.constraint(equalToConstant: 100),
redBox.centerXAnchor.constraint(equalTo: view.centerXAnchor),
redBox.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
}with this
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let yellow = UIView()
yellow.backgroundColor = .yellow
view.addSubview(yellow, constraints: Constraints.pin)
let redBox = UIView()
redBox.backgroundColor = .red
view.addSubview(redBox,
constraints: Constraints.center,
singleConstraints: Constraints.fixedSize(size: CGSize(width: 100, height: 100)))
}
}- iOS 12.0+
- Xcode 10.0+
- Swift 5.0+
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsCocoaPods 1.1.0+ is required to build PrettyLayout.
To integrate SnapKit into your Xcode project using CocoaPods, specify it in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
target '<Your Target Name>' do
pod 'PrettyLayout'
endThen, run the following command:
$ pod installIf you prefer not to use either of the aforementioned dependency managers, you can integrate PrettyLayout into your project manually.
view.addSubview(label, constraints: [
equal(\.topAnchor)
equal(\.leadingAnchor),
equal(\.trailingAnchor)
])You can also describe relationship to other child, constants, and priority.
view.addSubview(button, constraints: [
equal(\.topAnchor, to: label, \.bottomAnchor, constant: 16, priority: .defaultHigh)
equal(\.leadingAnchor),
equal(\.trailingAnchor)
])In this example button was added to view under the label with offset.
equallessOrEqualgreaterOrEqual
You can use preset function for reduce the code:
Constraints.pinConstraints.centerConstraints.with(insets:)Constraints.fixedSizeRespectBottomSafeAreaConstraints.pinRespectBottomSafeAreaConstraints.with(insets:)RespectSafeAreaConstraints.pinRespectSafeAreaConstraints.with(insets:)
PrettyLayout is released under the MIT license. See LICENSE for details.