QuickLayout
Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.
Install / Use
/learn @huri000/QuickLayoutREADME
QuickLayout

QuickLayout offers an additional way, to easily manage the Auto Layout using only code. You can harness the power of QuickLayout to align your interface programmatically without even creating constraints explicitly.
- The WHY
- Naming Convension
- Features
- Example Project
- Requirements
- Installation
- Usage
The WHY
Why should you use QuickLayout?
- QuickLayout drastically shortens the amount of code in case you ever need to write the view hierarchy.
- It provides a common Auto Layout API for iOS, tvOS and macOS.
- QuickLayout contains most of the Auto Layout constructs an iOS App requires.
- The QuickLayout method declarations are very descriptive and clear. It is fully documented!
- Layout a
UIVieworNSViewor an array of views using the instances themselves, without even creating a single NSLayoutConstraint instance.
Naming Convension
As of version 2.0.0, QuickLayout supports tvOS and macOS as well as iOS. Therefore, a few adjustments have been made.
QLViewreplacesUIVieworNSView.QLPriorityreplacesNSLayoutConstraint.PriorityandUILayoutPriorityQLAttributereplacesNSLayoutConstraint.AttributeandNSLayoutAttributeQLRelationreplacesNSLayoutConstraint.RelationandNSLayoutRelation
Features
- Extension to
QLViewthat contains functionality that allows you to set constraints directly from the view itself. - Extension to
Array of QLViewthat contains functionality that allows you to set constraints directly from an array of views.
Example Project
The example project demonstrates the benefits of using QuickLayout with several common use cases. Have a look! 😎
Requirements
Swift 4.0 or any higher version.
Installation
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate QuickLayout into your Xcode project using CocoaPods, specify the following in your Podfile:
pod 'QuickLayout', '3.0.2'
Then, run the following command:
$ pod install
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate QuickLayout into your Xcode project using Carthage, specify the following in your Cartfile:
github "huri000/QuickLayout" == 3.0.2
Swift Package Manager
The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
Using Xcode 11.0+ go to your project file and enter the project URL of this repository:
https://github.com/huri000/QuickLayout
Accio
Accio is a decentralized dependency manager driven by SwiftPM that works for iOS/tvOS/watchOS/macOS projects.
You can install Accio with Homebrew using the following command:
$ brew tap JamitLabs/Accio https://github.com/JamitLabs/Accio.git
$ brew install accio
To integrate QuickLayout into your Xcode project using Accio, specify the following in your Package.swift manifest:
.package(url: "https://github.com/huri000/QuickLayout.git", .exact("3.0.2"))
After specifying "QuickLayout" as a dependency of the target in which you want to use it, run accio install.
Manually
Add the source files to your project.
Usage
Using QuickLayout is easy. No setup or preparation is required.
All the necessary methods are already available in any of the QLView instances, and are fully documented and highly descriptive.
First, some boilerplate code: Define simpleView of type QLView and add it to the view hierarchy.
// Create a view, add it to view hierarchy, and customize it
let simpleView = QLView()
simpleView.backgroundColor = .gray
parentView.addSubview(simpleView)
Constant edges
Set a constant edge of a view:
simpleView.set(.height, of: 50)
You can set multiple constant edges using variadic parameters:
simpleView.set(.width, .height, of: 100)
Layout to Superview
You can easily layout a view directly to its superview as long as it has one.
Layout edge-x to superview edge-x
Layout the top of a view to the top of its superview:
simpleView.layoutToSuperview(.top)
Layout the centerX of a view to the centerX of its superview:
simpleView.layoutToSuperview(.centerX)
Multiple edges
You can also layout multiple edges likewise, using variadic parameters:
simpleView.layoutToSuperview(.top, .bottom, .centerX, .width)
Using the applied constraint
All the layout methods return the applied constraints, but the returned values are discardable so you can simply ignore them if you don't need them.
let topConstraint = simpleView.layoutToSuperview(.top)
// Change the offset value by adding 10pts to it
topConstraint.constant += 10
Ratio
You can layout a view to 80% its superview width:
simpleView.layoutToSuperview(.width, ratio: 0.8)
Offset
You can layout a view to it's superview width minus 10pts offset:
simpleView.layoutToSuperview(.width, offset: -10)
Center
Layout view to the center of superview:
let center = simpleView.centerInSuperview()
You can optionally retreive the returned QLCenterConstraints instance.
center?.y.constant = 20
That is the equivalent of doing the following, without getting the QLCenterConstraints instance (but an array of NSLayoutConstraint instead).
simpleView.layoutToSuperview(.centerX, .centerY)
Size
Size to superview with optional ratio - It means that simpleView is 80% its superview size.
let size = simpleView.sizeToSuperview(withRatio: 0.8)
You can optionally retreive the returned QLSizeConstraints instance.
size?.width.constant = -20
That is the equivalent of doing the following, without getting the QLSizeConstraints instance (but an array of NSLayoutConstraint instead).
simpleView.layoutToSuperview(.width, .height, ratio: 0.8)
Fill
let fillConstraints = simpleView.fillSuperview()
You can optionally retreive the returned QLFillConstraints instance.
fillConstraints?.center.y.constant = 5
Axis:
You can layout view to a certain axis, for example:
Horizontally:
let axis = simpleView.layoutToSuperview(axis: .horizontally)
Vertically:
simpleView.layoutToSuperview(axis: .vertically)
That is equivalent to (Horizontally):
simpleView.layoutToSuperview(.left, .right)
Or (Vertically):
simpleView.layoutToSuperview(.top, .bottom)
You can reteive the QLAxisConstraints instance as well and use it.
Layout to View
It is p
