Latest 3.3.1
Homepage https://github.com/roberthein/TinyConstraints
License MIT
Platforms ios 9.0, tvos 9.0, osx 10.11
Authors

TinyConstraints

tl;dr Nothing but sugar.

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

TinyConstraintsTinyConstraintsTinyConstraints

Features

  • [X] Pure Swift 4 sweetness.
  • [X] Shortest possible syntax for creating layout constraints.
  • [X] Constraints are active by default.
  • [X] 100% compatible with Auto Layout.
  • [X] Optionally store your constraints.
  • [X] Set constraint priorities upon creation.
  • [X] Constrain directly to the superview.
  • [X] Stack views together with one line of code.
  • [X] No need to set translatesAutoresizingMaskIntoConstraints because TinyConstraints does it for you.

Examples

Edges

Attaching a view to its superview with NSLayoutConstraint:

NSLayoutConstraint.activate([
    view.topAnchor.constraint(equalTo: superview.topAnchor, constant: 0),
    view.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 0),
    view.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: 0),
    view.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: 0)
])

with TinyConstraints:

view.edgesToSuperView()

or:

view.edgesToSuperView(insets: .top(10) + .left(10))

Center

Constraining the center of a view to its superview with NSLayoutConstraint:

NSLayoutConstraint.activate([
    view.centerXAnchor.constraint(equalTo: superview.centerXAnchor, constant: 0)
    view.centerYAnchor.constraint(equalTo: superview.centerYAnchor, constant: 0)
])

with TinyConstraints:

view.center(in: superview)

or:

view.center(in: superview, offset: CGPoint(x: 10, y: 10))

Basic Use

Typealiases

TinyConstraints gives you convenient and tiny typealiases for handling constraints.

  • Constraint = NSLayoutConstraint
  • Constraints = [NSLayoutConstraint]

Equal and Unequal Anchors

This constraints the top-anchor of the view to the top-anchor of the superview:

view.top(to: superview)

This constraints the top-anchor of firstView to the bottom-anchor of secondView:

firstView.topToBottom(of: secondView)

Constrain to Superview

Often you need to constrain a view to it’s superview, with TinyConstraints you can do this super easy:

view.edgesToSuperview()

Or only one edge:

view.topToSuperview()

Or you can attach all edges except one, like this:

view.edgesToSuperview(excluding: .bottom)

Relation and Priority

For almost all constraints you can set the relation and priority properties. The default relation is .equal and the default priority is .required:

container.width(150, relation: .equalOrLess, priority: .high)

Storing Constraints

Here we create a set of inactive constraints and store these to our property:

let constraints = view.size(CGSize(width: 100, height: 100), isActive: false)

Activation and Deactivation

Besides the default NSLayoutConstraint activation, TinyConstraints also provides a way to activate a set of constraints:

constraints.activate()

You can also do this in an animation:

oldConstraints.deActivate()

constraints.activate()
UIViewPropertyAnimator(duration: 1, dampingRatio: 0.4) {
    self.layoutIfNeeded()
}.startAnimation()

Animating Constraint Constants

Here we add a height constraint to a view, store it and animate it later:

let height = view.height(100)

height.constant = 200
UIViewPropertyAnimator(duration: 1, dampingRatio: 0.4) {
    self.layoutIfNeeded()
}.startAnimation()

Stack

Stack provides a way of constraining views together in a superview:

let views = [logo, title, description]
superview.stack(views, axis: .vertical, spacing: 10)
Find these examples and more in the Example Project.

Installation

CocoaPods

TinyConstraints is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "TinyConstraints"

Carthage

TinyConstraints is available through Carthage. To install
it, simply add the following line to your Cartfile:

github "roberthein/TinyConstraints"

Suggestions or feedback?

Feel free to create a pull request, open an issue or find me on Twitter.

Latest podspec

{
    "name": "TinyConstraints",
    "version": "3.3.1",
    "summary": "Nothing but sugar.",
    "description": "Tiny Constraints is the syntactic sugar that makes NSLayoutConstraint sweeter for human use.",
    "homepage": "https://github.com/roberthein/TinyConstraints",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Robert-Hein Hooijmans": "[email protected]"
    },
    "source": {
        "git": "https://github.com/roberthein/TinyConstraints.git",
        "tag": "3.3.1"
    },
    "social_media_url": "https://twitter.com/roberthein",
    "platforms": {
        "ios": "9.0",
        "tvos": "9.0",
        "osx": "10.11"
    },
    "source_files": "TinyConstraints/Classes/**/*.{swift}"
}

Pin It on Pinterest

Share This