Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.

Overview

QuickLayout

Platform Language Version Carthage Compatible SwiftPM Compatible Accio: Supported codecov License

image

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

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 UIView or NSView or 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.

  • QLView replaces UIView or NSView.
  • QLPriority replaces NSLayoutConstraint.Priority and UILayoutPriority
  • QLAttribute replaces NSLayoutConstraint.Attribute and NSLayoutAttribute
  • QLRelation replaces NSLayoutConstraint.Relation and NSLayoutRelation

Features

  • Extension to QLView that contains functionality that allows you to set constraints directly from the view itself.
  • Extension to Array of QLView that 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 possible to layout one view to another inside the view hierarchy.

Layout edge-x to edge-y of another view

You can layout an edge of a view to another. For example:

Layout simpleView's left edge to the right edge of anotherView, with 20pts right offset.

simpleView.layout(.left, to: .right, of: anotherView, offset: 20)

Edge-x to edge-x of another view

Layout simpleView's top edge to the top edge of anotherView

simpleView.layout(to: .top, of: anotherView)

Multiple edges

Layout simpleView's left, right and centerY to anotherView's left, right and centerY, respectively.

simpleView.layout(.left, .right, .centerY, to: anotherView)

Content Wrap

Content Hugging Oriority and Content Compression Resistance can be also mutated in code

Vertical example:

let label = UILabel()
label.text = "Hi There!"
label.verticalCompressionResistancePriority = .required
label.verticalHuggingPriority = .required

You can set the compression resistence and the hugging priority, together. Thus, forcing both to be .required vertically and horizontally.

label.forceContentWrap()

You can force content wrap a specific axis:

label.forceContentWrap(.vertically)

Array of QLView Elements

You can generate an array of views and apply constraints on them all in one shot.

// Create array of views and customize it
var viewsArray: [QLView] = []
for _ in 0...4 {
    let simpleView = QLView()
    view.addSubview(simpleView)
    viewsArray.append(simpleView)
}

Constant edges

Each element gets height of 50pts, using this single line of code.

viewsArray.set(.height, of: 50)

Axis

Each element cling to left and right of its superview.

viewsArray.layoutToSuperview(axis: .horizontally, offset: 30)

Multiple edges

Each element left, right, top, bottom edges is exactly fits another view.

viewsArray.layout(.left, .right, .top, .bottom, to: parentView)

Spread views

You can spread the elements one below the other (vertically), the first stretches to the top of the superview and the last stretchs to the bottom of the superview. There is an offset of 1pt between each element.

viewsArray.spread(.vertically, stretchEdgesToSuperview: true, offset: 1)

More

Every layout method has several optional parameters - see below:

Priority

  • The priority of the applied constraint.
  • Included by all the layout methods.
  • Default value: .required.

Other than the default system priorities, QuickLayout offers one more - it has 999 value and it's called .must.

You can tweak the priorities as you like in order to deal with breakage and redundancies.

Example for setting the constraints priority:

let width = simpleView.set(.width, of: 100, priority: .must)

Relation

  • The relation of a view to another view.
  • Included by most of the layout methods.
  • Default value: .equal

Ratio

  • The ratio of a view to another view.
  • Included by most of the layout methods.
  • Default value: 1

Offset

  • The offset of a view to another view.
  • Included by most of the layout methods.
  • Default value: 0

Explicit Layout

You can layout a view/s explicitly to a superview or another view when you need to. Most paramters have a default value.

simpleView.layout(.height, to: .width, of: anotherView, relation: .lessThanOrEqual, ratio: 0.5, offset: 10, priority: .defaultHigh)

Contributing

Forks, patches and other feedback are welcome.

Author

Daniel Huri ([email protected])

License

QuickLayout is available under the MIT license. See the LICENSE file for more info.

Comments
  • Set Xcode build system to `new`

    Set Xcode build system to `new`

    Issue Link πŸ”—

    https://github.com/huri000/QuickLayout/issues/38

    Goals πŸ₯…

    Re-enables Carthage build using Xcode 13

    How to Test

    1. Create empty xcode project
    2. Create Cartfile with content github "Blackjacx/QuickLayout" "master"
    3. Run carthage build --no-skip-current --use-xcframeworks

    Doing the same when the Cartfile instead contains github "Blackjacx/QuickLayout" (latest tag) fails

    Important

    When this has been changed please also update the reference of SwiftEntryKit to the new release so we can actually use it. Thanks a lot :)

    opened by Blackjacx 5
  • Swift PM

    Swift PM

    Goals πŸ₯…

    • Add support for Swift Package Manager

    Implementation Details ✏️

    • Added Package.swift
    • Details added to README on how to include in your project.
      • This can be added without Xcode 11, but that is the easiest implementation.

    Testing Details πŸ”

    • I checked that everything compiled when a package was included into a project
    opened by maxxfrazer 3
  • Cannot build using Carthage with Xcode 13 (Beta)

    Cannot build using Carthage with Xcode 13 (Beta)

    Describe the bug

    Using Xcode 13 QuickLayout is not able to compile using the command carthage bootstrap --use-ssh --platform iOS --cache-builds which worked flawlessly before. We install version 3.0.0 (which is automatically installen when specifying SwiftEntryKit in the Cartfile).

    In the Xcode build logs I get a ** BUILD SUCCEEDED ** [2.570 sec] but later a ** ARCHIVE FAILED **

    I often see the messages remark: Incremental compilation has been disabled: it is not compatible with whole module optimization and error: The Legacy Build System will be removed in a future release. You can configure the selected build system and this deprecation message in File > Workspace Settings. in the build logs.

    Not exactly sure where carthage is getting this info from and how Carthage is building your project (because I found no workspace in the root folder) but the example workspace has legacy build enabled... It would be cool if you can use Xcode's default build system which might resolve the error...

    Edit

    I cloned the repo, set the build system to new and executed carthage build --no-skip-current --use-xcframeworks. Works flawlessly πŸŽ‰

    Visuals

    *** Building scheme "QuickLayout" in QuickLayout.xcworkspace
    Build Failed
    	Task failed with exit code 65:
    	/usr/bin/xcrun xcodebuild -workspace <project_root>/Carthage/Checkouts/QuickLayout/Example/QuickLayout.xcworkspace -scheme QuickLayout -configuration Release -derivedDataPath ~/Library/Caches/org.carthage.CarthageKit/DerivedData/13.0_13A5154h/QuickLayout/3.0.0 -sdk iphoneos ONLY_ACTIVE_ARCH=NO CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= CARTHAGE=YES archive VALIDATE_WORKSPACE=NO -archivePath /var/folders/s8/6bfvlm916_94n26jl90s1tmw0000gn/T/QuickLayout SKIP_INSTALL=YES GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=NO CLANG_ENABLE_CODE_COVERAGE=NO STRIP_INSTALLED_PRODUCT=NO (launched in <project_root>/Carthage/Checkouts/QuickLayout)
    
    This usually indicates that project itself failed to compile. Please check the xcodebuild log for more details: /var/folders/s8/6bfvlm916_94n26jl90s1tmw0000gn/T/carthage-xcodebuild.07vv56.log
    

    Necessary context

    • Xcode Version 13
    • Swift Version 5.x
    opened by stherold 2
  • Updating project

    Updating project

    Issue Link πŸ”—

    A link to the issue & short explanation

    Goals πŸ₯…

    What have I tried to acheive / improve + use case example

    Implementation Details ✏️

    Testing Details πŸ”

    How did I test my implementation

    opened by mnikolic-sh 0
  • Adding SwiftLint

    Adding SwiftLint

    Version bump: 2.0.1 Added conformance to SwiftLint + New lint rules

    Issue Link πŸ”—

    XXX

    Goals πŸ₯…

    Linting QuickLayout

    Implementation Details ✏️

    Added swiftlint + rules

    Testing Details πŸ”

    XXX

    opened by huri000 0
  • Adding SwiftLint

    Adding SwiftLint

    Issue Link πŸ”—

    XXX

    Goals πŸ₯…

    Adding lint rules using SwiftLint.

    Implementation Details ✏️

    Added .swiftlint.yml with enabled additional rules

    Testing Details πŸ”

    By running existing unit-tests.

    opened by huri000 0
  • Xcode 12.0 (12A7209) β€” could not build Objective-C module 'QuickLayout'

    Xcode 12.0 (12A7209) β€” could not build Objective-C module 'QuickLayout'

    QuickLayout is included in my project due to the use of SwiftEntryKit in CocoaPods. However, now with Xcode 12.0 I get an error I haven't gotten before:

    /Users/Jeroen/Library/Developer/Xcode/DerivedData/MyClient-afqeaqtuibsijpepvsfjjretfqvj/Build/Intermediates.noindex/Pods.build/MyClient Test-iphoneos/QuickLayout.build/module.modulemap:9:12: error: header 'QuickLayout-Swift.h' not found header "QuickLayout-Swift.h" ^ <unknown>:0: error: could not build Objective-C module 'QuickLayout'

    opened by JeroenJK 0
Owner
Daniel Huri
@daniel-huri at @facebook. Ex @blockchain
Daniel Huri
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast

Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]

layoutBox 2.1k Jan 2, 2023
An Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

KVConstraintKit KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift Installation Using CocoaPods

Keshav Vishwkarma 90 Sep 1, 2022
Auto Layout made easy with the Custom Layout.

Auto Layout made easy with the Custom Layout. Getting started CocoaPods CocoaPods is a dependency manager for Cocoa projects. You can install it with

Malith Nadeeshan 1 Jan 16, 2022
Auto Layout (and manual layout) in one line.

Auto Layout (and manual layout) in one line. Quick Look view.bb.centerX().below(view2).size(100) It’s equivalent to iOS 9 API: view.centerXAnchor.cons

Javier Zhang 74 Oct 19, 2022
Auto Layout In Swift Made Easy

Swiftstraints Swiftstraints can turn verbose auto-layout code: let constraint = NSLayoutConstraint(item: blueView, attr

null 119 Jan 29, 2022
Auto Layout made easy

EasyPeasy is a Swift framework that lets you create Auto Layout constraints programmatically without headaches and never ending boilerplate code. Besi

Carlos Vidal 1.9k Dec 23, 2022
Easy Auto Layout

RKAutoLayout Easy AutoLayout TL;DR let view1: UIView = UIView() let view2: UIView = UIView() view1.addSubview(view2) /// Add all view1.rk_alAdd(

Roman Kotov 1 Mar 24, 2019
The ultimate API for iOS & OS X Auto Layout β€” impressively simple, immensely powerful. Objective-C and Swift compatible.

The ultimate API for iOS & OS X Auto Layout β€” impressively simple, immensely powerful. PureLayout extends UIView/NSView, NSArray, and NSLayoutConstrai

PureLayout 7.6k Jan 6, 2023
Declarative Auto Layout in Swift, clean and simple

Tails Tails is a take on declarative Auto Layout. If you don't like typing (like me), it might be your kind of thing! Tails is written in Swift and cu

Nick Tymchenko 17 Jan 31, 2019
Very simple swipe-to-dismiss, supporting Auto Layout and dynamic heights

PanelPresenter Add swipe-dismiss logic to your view controller, supporting Auto Layout and dynamic heights. Installation Add this package to your proj

Pim 3 Aug 23, 2022
Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout

Masonry Masonry is still actively maintained, we are committed to fixing bugs and merging good quality PRs from the wider community. However if you're

null 18k Jan 5, 2023
Intuitive and powerful Auto Layout library

Align introduces a better alternative to Auto Layout anchors. Semantic. Align APIs focus on your goals, not the math behind Auto Layout constraints. P

Alexander Grebenyuk 338 Oct 18, 2022
A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:

Cartography ?? ?? Using Cartography, you can set up your Auto Layout constraints in declarative code and without any stringly typing! In short, it all

Robb BΓΆhnke 7.3k Jan 4, 2023
Lightweight Swift framework for Apple's Auto-Layout

I am glad to share with you a lightweight Swift framework for Apple's Auto-Layout. It helps you write readable and compact UI code using simple API. A

null 349 Dec 20, 2022
A compact but full-featured Auto Layout DSL for Swift

Mortar allows you to create Auto Layout constraints using concise, simple code statements. Use this: view1.m_right |=| view2.m_left - 12.0 Instead of:

Jason Fieldman 83 Jan 29, 2022
Yet Another Swift Auto Layout DSL

FormationLayout Documentation FormationLayout is the top level layout class for one root view. FormationLayout takes a UIView as its rootView. transla

Evan Liu 53 Mar 31, 2022
Lightweight declarative auto-layout framework for Swift

SwiftyLayout SwiftyLayout is a framework that allows to describe layout constraints (ie NSLayoutConstraint) as a simple mathematical formula in a Swif

Hisakuni Fujimoto 15 Nov 7, 2017
Minimal Auto Layout in Swift

Restraint Restraint is a very very small library to help make your use of NSLayoutConstraint in Swift more legible & declarative. Like programmatic vi

The Puffin Supply Project 80 Aug 23, 2022
Swift microframework for declaring Auto Layout constraints functionally

Relayout Relayout is a Swift microframework to make using Auto Layout easier with static and dynamic layouts. Why? If you want to build a UI using App

Steve Streza 560 Nov 19, 2022