Latest 0.6
Homepage https://github.com/ObjColumnist/ConstrainTo
License BSD (3-clause)
Platforms ios 8.0, requires ARC
Frameworks UIKit
Authors

Carthage compatible
Cocoapods Compatible

ConstrainTo is a simple and tiny extension to UIView, that aims to simplify the common use cases of AutoLayout without trying to introduce new paradigms.

ConstrainTo removes a lot of boiler plate code by automatically:

  • Setting translatesAutoresizingMaskIntoConstraints to false on the view being constrained
  • Automatically activating created constraints by setting isActive to true
  • Returning all constraints that have been created in case you need access to them in the future

ConstrainTo has 2 main methods:

@discardableResult public func constrain(_ attribute: NSLayoutAttribute, being relation: NSLayoutRelation = .equal, to viewAttribute: NSLayoutAttribute, of view: UIView, multipliedBy multiplier: CGFloat = 1.0, offsetBy offset: CGFloat = 0.0, activate: Bool = true, priority: Float = UILayoutPriorityRequired) -> NSLayoutConstraint

@discardableResult public func constrain(_ attribute: NSLayoutAttribute, being relation: NSLayoutRelation = .equal, to constant: CGFloat, activate: Bool = true, priority: Float = UILayoutPriorityRequired) -> NSLayoutConstraint

In addition to 7 convenience methods:

@discardableResult public func constrain(to attribute: NSLayoutAttribute, of view: UIView, multipliedBy multiplier: CGFloat = 1.0, offsetBy offset: CGFloat = 0.0) -> NSLayoutConstraint

@discardableResult public func constrain(to size: CGSize) -> (width: NSLayoutConstraintConstraint, heightConstraint: NSLayoutConstraint)

@discardableResult public func constrain(to view: UIView, insetBy insets: UIEdgeInsets = UIEdgeInsets.zero) -> (topConstraint: NSLayoutConstraint, leftConstraint: NSLayoutConstraint, bottomConstraint: NSLayoutConstraint, rightConstraint: NSLayoutConstraint)

@discardableResult public func constrain(toEdgesOf view: UIView, insetBy insets: UIEdgeInsets = UIEdgeInsets.zero) -> (topConstraint: NSLayoutConstraint, leadingConstraint: NSLayoutConstraint, bottomConstraint: NSLayoutConstraint, trailingConstraint: NSLayoutConstraint)

@discardableResult public func constrain(toMarginsOf view: UIView, insetBy insets: UIEdgeInsets = UIEdgeInsets.zero) -> (topMarginConstraint: NSLayoutConstraint, leadingMarginConstraint: NSLayoutConstraint, bottomMarginConstraint: NSLayoutConstraint, trailingMarginConstraint: NSLayoutConstraint)

@discardableResult public func constrain(toCenterOf view: UIView, offsetBy offsets: CGPoint = CGPoint.zero) -> (xConstraint: NSLayoutConstraint, yConstraint: NSLayoutConstraint)

@discardableResult public func constrain(toSizeOf view: UIView) -> (widthConstraint: NSLayoutConstraint, heightConstraint: NSLayoutConstraint)

Examples

Constrain an attribute to the attribute of another view

If you wanted the left of redView to be to the right of blueView you would need to write:

redView.constrain(.left, being: .equal, to: .right, of: blueView, multipliedBy: 1, offsetBy: 0, activate: true, priority: UILayoutPriorityRequired)

But because of default parameters you just need to write:

redView.constrain(.left, to: .right, of: blueView)

If you wanted the left of redView to be 10 pts to the right of blueView you would need to write:

redView.constrain(.left, to: .right, of: blueView, offsetBy: 10)

Constrain an attribute

If you want to constrain the width of redView to 20 pts you would need to write:

redView.constrain(.width, being: .equal, to: 20, activate: true, priority: UILayoutPriorityRequired)

But because of default parameters you just need to write:

redView.constrain(.width, to: 20)

Constrain an view to the same attribute of another view

If you want to constrain the width of redView to the width of blueView you would write:

redView.constrain(to: .width, of: blueView, multipliedBy: 1, offsetBy: 0)

But because of default parameters you just need to write:

redView.constrain(to: .width, of: blueView)

Constrain size

If you want to constrain the size of redView to 20 x 20 pts you would need to write:

redView.constrain(to: CGSize(width: 20, height: 20))

Constrain to another view’s frame

If you want to constrain the frame (top, left, bottom and right) of redView to that of blueView you would need to write:

redView.constrain(to: blueView, insetBy: UIEdgeInsets.zero)

But because of default parameters you just need to write:

redView.constrain(to: blueView)

If you want to constrain the frame of redView to be inset 10 pts to all edges of blueView you would need to write:

redView.constrain(to: blueView, insetBy: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))

Constrain to another view’s edges

If you want to constrain the edges (top, leading, bottom and trailing) of redView to that of blueView you would need to write:

redView.constrain(toEdgesOf: blueView, insetBy: UIEdgeInsets.zero)

But because of default parameters you just need to write:

redView.constrain(toEdgesOf: blueView)

If you want to constrain the edges of redView to be inset 10 pts to all edges of blueView you would need to write:

redView.constrain(toEdgesOf: blueView, insetBy: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))

Constrain to another view’s margins

If you want to constrain the margins (top margin, leading margin, bottom margin and trailing margin) of redView to that of blueView you would need to write:

redView.constrain(toMarginsOf: blueView, insetBy: UIEdgeInsets.zero)

But because of default parameters you just need to write:

redView.constrain(toMarginsOf: blueView)

If you want to constrain the margin’s of redView to be inset 10 pts to all edges of blueView you would need to write:

redView.constrain(toMarginsOf: blueView, insetBy: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))

Constrain to the center of another view

If you want to constrain redView to be centered in blueView you would need to write:

redView.constrain(toCenterOf: blueView, offsetBy: CGPoint.zero)

But because of default parameters you just need to write:

redView.constrain(toCenterOf: blueView)

If you want to constrain the center of redView to be 10 pts to below the center blueView you would need to write:

redView.constrain(toCenterOf: blueView, offsetBy: CGPoint(x: 0, y: 10))

Constrain to another view’s size

If you want to constrain the size of redView to the size of blueView you would need to write:

redView.constrain(toSizeOf: blueView)

Latest podspec

{
    "name": "ConstrainTo",
    "platforms": {
        "ios": "8.0"
    },
    "summary": "ConstrainTo is a simple and tiny AutoLayout DSL",
    "version": "0.6",
    "homepage": "https://github.com/ObjColumnist/ConstrainTo",
    "authors": {
        "Spencer MacDonald": "[email protected]"
    },
    "license": {
        "type": "BSD (3-clause)",
        "file": "LICENSE"
    },
    "source": {
        "git": "https://github.com/ObjColumnist/ConstrainTo.git",
        "tag": "0.6"
    },
    "source_files": "ConstrainTo/*.swift",
    "frameworks": "UIKit",
    "requires_arc": true
}

Pin It on Pinterest

Share This