Latest 3.0.1
Homepage https://github.com/hellostu/Pin
License MIT
Platforms ios 8.0, tvos 9.0, requires ARC
Authors

[![CI Status](http://img.shields.io/travis/Stuart Lynch/Pin.svg?style=flat)](https://travis-ci.org/Stuart Lynch/Pin)
Version
License
Platform

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Pin was designed to simplify AutoLayout code. It is intended to be easier to read than
NSLayoutConstraint code. Here is some typical AutoLayout code which matches
a view’s width to it’s parent’s width:

let view = UIView()
self.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
let constraint = NSLayoutConstraint(item: view, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0.0)
view.addConstraint(constraint)

Here is the same example with Pin:

let view = UIView()
self.addSubview(view)
Pin(view: view).width().constrain()

Notice that ‘view.translatesAutoresizingMaskIntoConstraints = false’ is not required with Pin.
All Pin constaints must terminate with a call to the constrain() function. Pin is so simple because it makes assumptions that are true for the majority of layout constraints you
are likely to set. These are as follows:

  1. You are applying the constraint on the view’s superview.
  2. The attribute you are setting, you want to match on both views (i.e. view’s width == superview width)
  3. You are using an equality relation
  4. The multiplier = 1.0 and the constant = 0.0

If these assumptions aren’t true, you can still set the additional properties using Pin.

Relating to Other Views

If you want the top of one view to equal the bottom of another view:

let otherView = UIView()
self.addSubview(otherView)
...

let view = UIView()
self.addSubview(view)
Pin(view: view).top().to(otherView).bottom().constrain()

You can read this constraint as Pin -> view.top -> to -> otherView.bottom.

Multipliers and Constants

If you want to set a multiplier and a constant you can do that with the following:

Pin(view: view).width().multiplier(0.5).constant(10.0).constrain()

This will constrain the width to be half the parent’s width + 10pts.

Width and Height Fixed Constraints

Setting width and height using only NSLayoutConstraints is particularly verbose.
Using pin you can do the following:

Pin(view: view).width(50.0).constrain()
Pin(view: view).height(100.0).constrain()

Change Relation

Pin assumes that the constraint relation is NSLayoutRelation.Equal. However if you
want to set a different relation, you can use the relation(…) function:

Pin(view: view).width(multiplyBy: 0.5).relation(.LessThanOrEqual).constrain()

Constraint Priority

Constraint priority is a property on NSLayoutConstraint and cannot be set from
the init function. This adds additional code lines. It’s simple with Pin:

Pin(view: view).width(multiplyBy: 0.5).relation(.LessThanOrEqual).constrain()
Pin(view: view).width(200.0).priority(750).constrain()

This constraint will set the width of view to be 200, only if it’s width would
be less than half of it’s parent.

Installation

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

pod "Pin"

Author

Stuart Lynch, [email protected]

License

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

Latest podspec

{
    "name": "Pin",
    "version": "3.0.1",
    "summary": "Pin is a Swift wrapper for AutoLayout",
    "description": "For those that have a preference to do UI code in iOS programatically rather than using Interface Builder, NSLayoutConstraints can take up a lot of lines of code. They can also be quite difficult to read and make sense of. The goal of this is to restrict the length required to express a layout constraint. It also makes the constraint easier to ready. This library is aimed for Swift use. It won't be as pleasent in Objective C, due to the builder chaining of functions.",
    "homepage": "https://github.com/hellostu/Pin",
    "license": "MIT",
    "authors": {
        "Stuart Lynch": "[email protected]"
    },
    "source": {
        "git": "https://github.com/hellostu/Pin.git",
        "tag": "3.0.1"
    },
    "platforms": {
        "ios": "8.0",
        "tvos": "9.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*"
}

Pin It on Pinterest

Share This