Home

Awesome

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

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

operatorlhsrhsvaluesemantics
+ConstraintTermCONSTANTConstraintTermadd rhs value to lhs.constant
+CONSTANTConstraintTermConstraintTermadd lhs value to rhs.constant
-ConstraintTermCONSTANTConstraintTermsubtract rhs value from lhs.constant
*ConstraintTermCONSTANTConstraintTermmultiply rhs value to lhs.multiplier and lhs.constant
*CONSTANTConstraintTermConstraintTermmultiply lhs value to rhs.multiplier and rhs.constant
/ConstraintTermCONSTANTConstraintTermdivide lhs.multiplier and lhs.constant by rhs value
==ConstraintTermConstraintTermNSLayoutConstraintcreate a layout constraint that "lhs is equal to lhs"
==ConstraintTermCONSTANTNSLayoutConstraintditto
==CONSTANTConstraintTermNSLayoutConstraintditto
<=ConstraintTermConstraintTermNSLayoutConstraintcreate a layout constraint that "lhs is less than or equal to lhs"
<=ConstraintTermCONSTANTNSLayoutConstraintditto
<=CONSTANTConstraintTermNSLayoutConstraintditto
>=ConstraintTermConstraintTermNSLayoutConstraintcreate a layout constraint that "lhs is greater than or equal to lhs"
>=ConstraintTermCONSTANTNSLayoutConstraintditto
>=CONSTANTConstraintTermNSLayoutConstraintditto
~NSLayoutConstraintCONSTANT(Float)NSLayoutConstraintChange the priority of a layout constraint, and return the constraint

Requirements

Installation

There are two options.

Using Carthage

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

Copying source files directly (iOS 7)

$ git submodule add https://github.com/fhisa/SwiftyLayout.git PATH_TO_SUBMODULE
// or
$ carthage update --use-submodules

TODO

license

SwiftyLayout is released under the MIT license.