PrettyConstraints helps you create your views faster programmatically using AutoLayout constraints.
Using CocoaPods, install it by adding the following line to your Podfile:
pod 'PrettyConstraints'
Using Swift Package Manager, add it as a Swift Package in Xcode 11.0 or later, select File > Swift Packages > Add Package Dependency...
and add the repository URL:
https://github.com/mobiraft/PrettyConstraints
Download and include the PrettyConstraints
folder and files in your codebase.
- iOS 11
- Swift 5
let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 100)))
view.backgroundColor = .black
let subView = UIView()
subView.backgroundColor = .gray
view.addSubview(subView)
subView.applyConstraints(.fitInView(view, inset: 10))
subView.applyConstraints(.center(in: view),
.width(constant: 50),
.height(constant: 50))
subView.applyConstraints(.top(to: view.topAnchor),
.bottom(to: view.bottomAnchor),
.leading(to: view.leadingAnchor, equality: .greaterThanOrEqual),
.trailing(to: view.trailingAnchor, equality: .lessThanOrEqual),
.centerX(to: view.centerXAnchor),
.width(constant: 50),
.height(constant: 50))
let constraints:[NSLayoutConstraint] = subView.applyConstraints(.center(in: view),
.width(constant: 50),
.height(constant: 50))
constraints[1].constant = 20 // Modifies the width as 20.0
- Create app without Storyboards using PrettyConstraints | Mobiraft
- iOS app without Storyboards using PrettyConstraints | by Hardik Parmar | Nov, 2020 | Medium
Visit us for more Android, SwiftUI and Swift articles. www.mobiraft.com