Lightweight declarative auto-layout framework for Swift

Related tags

Layout SwiftyLayout
Overview

SwiftyLayout

GitHub license Carthage compatible GitHub release build passing

Join the chat at https://gitter.im/fhisa/SwiftyLayout

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

About Version

  • SwiftyLayout 3 for Swift 2
  • SwiftyLayout 4 for Swift 3

Code Examples

Basic usage

Using the framework, a layout constraint that "the width of the view A is equal to minus 4.0 to 50% of the width of the Views B" follows:

viewA[.Width] == 0.5 * viewB[.Width] - 4.0

This is the same layout constraints with the following code:

NSLayoutConstraint(
    item: viewA, attribute: .Width,
    relatedBy: .Equal,
    toItem: viewB, attribute: .Width,
    multiplier: 0.5, constant: -4.0)

e.g. Aspect ratio

A layout constraint that "the aspect ratio of the view A is 3:4" follows:

viewA[.Width] * 3.0 == viewA[.Height] * 4.0

This is the same layout constraints with the following code:

NSLayoutConstraint(
    item: viewA, attribute: .Width,
    relatedBy: .Equal,
    toItem: viewA, attribute: .Height,
    multiplier: 4.0/3.0, constant: 0.0)

e.g. Specify the priority

The framework has priority specification operator ~ like the following.

innerView[.Leading] == outerView[.Leading] + 4.0 ~ 750.0

This is the same layout constraints with the following code:

var constraint = NSLayoutConstraint(
    item: innerView, attribute: .Leading,
    relatedBy: .Equal,
    toItem: outerView, attribute: .Leading,
    multiplier: 1.0, constant: 4.0)
constraint.priority = 750.0
// -> constraint

Please refer to the code for the sample application and test case, too.

Refrence Guide

ConstraintTerm

ConstraintTerm means a term that contains a view in the right side or the left side of a layout constraint. For example, a ConstraintTerm representing the width .Width of view viewA:

viewA[.Width]

viewA is a UIView instance object, .Width is a NSLayoutAttribute value. ConstraintTerm is defined as a structure such as the following:

public struct ConstraintTerm
{
    let view: UIView?
    let attribute: NSLayoutAttribute
    var multiplier: CGFloat = 1.0
    var constant: CGFloat = 0.0
}

Operators

The following table, CONSTANT means CGFloat value

operator lhs rhs value semantics
+ ConstraintTerm CONSTANT ConstraintTerm add rhs value to lhs.constant
+ CONSTANT ConstraintTerm ConstraintTerm add lhs value to rhs.constant
- ConstraintTerm CONSTANT ConstraintTerm subtract rhs value from lhs.constant
* ConstraintTerm CONSTANT ConstraintTerm multiply rhs value to lhs.multiplier and lhs.constant
* CONSTANT ConstraintTerm ConstraintTerm multiply lhs value to rhs.multiplier and rhs.constant
/ ConstraintTerm CONSTANT ConstraintTerm divide lhs.multiplier and lhs.constant by rhs value
== ConstraintTerm ConstraintTerm NSLayoutConstraint create a layout constraint that "lhs is equal to lhs"
== ConstraintTerm CONSTANT NSLayoutConstraint ditto
== CONSTANT ConstraintTerm NSLayoutConstraint ditto
<= ConstraintTerm ConstraintTerm NSLayoutConstraint create a layout constraint that "lhs is less than or equal to lhs"
<= ConstraintTerm CONSTANT NSLayoutConstraint ditto
<= CONSTANT ConstraintTerm NSLayoutConstraint ditto
>= ConstraintTerm ConstraintTerm NSLayoutConstraint create a layout constraint that "lhs is greater than or equal to lhs"
>= ConstraintTerm CONSTANT NSLayoutConstraint ditto
>= CONSTANT ConstraintTerm NSLayoutConstraint ditto
~ NSLayoutConstraint CONSTANT(Float) NSLayoutConstraint Change the priority of a layout constraint, and return the constraint

Requirements

  • Swift 2.0 (Xcode 7 or later)
  • iOS
    • iOS 8 or later / iOS 7 (by coping the source files directly)
  • Mac
    • Mac OS X 10.10 or later

Installation

There are two options.

Using Carthage

Using Carthage, it's easy to add SwiftyLayout to the project.

  • Add github "fhisa/SwiftyLayout" to your Cartfile.
  • Run carthage update
  • Add SwiftyLayout.framework in Carthage/Build/iOS to your Xcode project.

Copying source files directly (iOS 7)

  • Add this repository as a git submodule:
$ git submodule add https://github.com/fhisa/SwiftyLayout.git PATH_TO_SUBMODULE
// or
$ carthage update --use-submodules
  • Then just add references of SwiftyLayout/*.swift to your Xcode project.

TODO

license

SwiftyLayout is released under the MIT license.

You might also like...
Auto Layout In Swift Made Easy

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

Yet Another Swift Auto Layout DSL
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

Minimal Auto Layout in Swift
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

Swift microframework for declaring Auto Layout constraints functionally
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

SuperLayout is a Swift library that makes using Auto Layout a breeze.
SuperLayout is a Swift library that makes using Auto Layout a breeze.

SuperLayout is a library that adds a few custom operators to Swift that makes using the amazing NSLayoutAnchor API for Auto Layout a breeze. SuperLayo

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

QuickLayout QuickLayout offers an additional way, to easily manage the Auto Layout using only code. You can harness the power of QuickLayout to align

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

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]

Auto Layout made easy
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

TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use.
TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use.

TinyConstraints is the syntactic sugar that makes Auto Layout sweeter for human use. Features Pure Swift 5 sweetness. Everything you can do with Auto

Comments
Releases(v4.0.2)
Owner
Hisakuni Fujimoto
@rubycocoa creator
Hisakuni Fujimoto
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
⚓️ Declarative, extensible, powerful Auto Layout

EasyAnchor ❤️ Support my apps ❤️ Push Hero - pure Swift native macOS application to test push notifications PastePal - Pasteboard, note and shortcut m

Khoa 449 Nov 10, 2022
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
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 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
A declarative UIKit for improve layout productivity when developing an iOS application

TifoKit A declarative UIKit for improve layout productivity when developing an iOS application Requirements Min. iOS 11 Swift 5+ Installation Currentl

Tifo Audi A.P 22 Aug 9, 2022
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]

Extremely Fast views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainabl

layoutBox 2.1k Dec 22, 2022
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
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
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