Latest 1.1.4
License MIT
Platforms ios 8.0


Yet another programmatic constraints library for iOS. The focus of ClingConstraints is to have clean, readable, and powerful constraint creation.

For instance, thisView.copy(.height, of: thatView)


✅ One-line constraint creation
✅ One-line copy of multiple constraints
✅ Chainable property editing for constraints
✅ Clinging differing constraint attributes together
✅ One-line vertical or horizontal view filling
✅ Auto-disable translatesAutoresizingMaskIntoConstraints
✅ Automatically activate constraints
✅ NSLayoutConstraint collection mass activation/deactivation
❌ Something bad


  1. Install CocoaPods
  2. Add this repo to your Podfile

    target 'Example' do
        # IMPORTANT: Make sure use_frameworks! is included at the top of the file
        pod 'ClingConstraints'
  3. Run pod install in the podfile directory from your terminal
  4. Open up the .xcworkspace that CocoaPods created
  5. Done!


Copy another View’s Attribute
//  Creates and activates a constraint that makes thisView's height equal to thatView's
thisView.copy(.height, of: thatView)
Copying Constraints with Personal Space
// thisView copies thatView's height * 0.5 - 30.
thisView.copy(.height, of: thatView).withOffset(-30).withMultiplier(0.5)
Copying Multiple Constraints In-Line
// thisView copies the right, top, and left anchor constraints of that view-- in one line.
thisView.copy(.right, .top, .left, of: thatView)
Clinging Constraints Together
// thisView positions itself to the right of thatView with a spacing of 5
thisView.cling(.left, to: thatView, .right).withOffset(5)
Filling a View
// Fills a view from the top to the bottom with the given views
thisView.fill(.topToBottom, withViews: [thatView1, thatView2], spacing: 0)

What’s in the box?

Constraint Creation:

On any UIView, you can call the following functions.

Note that these all return the created constraint. If multiple constraints are created, a list of constraints are returned.

// This view copies the other view's attributes (returns list of created constraints)
copy(_: NSLayoutAttribute..., of: UIView)

// This view copies the other view's attribute
copy(_: NSLayoutAttribute, of: UIView)

// Clings the calling view's attribute to the other view's attribute.
cling(_: NSLayoutAttribute, to: UIView, _: NSLayoutAttribute)

// Fills the calling view with the given FillMethod from left to right.
// FillMethods: .leftToRight, .rightToLeft, .topToBottom, .bottomToTop
fill(_: FillMethod, withViews: [UIView], withSpacing: CGFloat, spacesInternally: Bool = true)

// Sets the height for this view
setHeight(_: CGFloat)

// Sets the width for this view
setWidth(_: CGFloat)
Constraint Property Chaining:

On any NSLayoutConstraint:

withMultiplier(_: CGFloat)
withOffset(_: CGFloat)
withPriority(_: UILayoutPriority)
withRelation(_: NSLayoutRelation)

For constraint activation and deactivation:

In any collection of constraints:


Example Project

The example project in this repository will show how the above animation was created using constraints. Clone this repository and open .xcodeproj file located in the "Example Project" directory.


Read the docs


Chris Perkins

Thanks to Luis Padron for helping set this up! 👍

Latest podspec

    "name": "ClingConstraints",
    "version": "1.1.4",
    "summary": "Library for programmatic constraints in iOS; emphasis on readability",
    "description": "A programmatic constraints library for iOS written using Swift 4.2.nEasily write constraints in code and keep them readable!",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Chris Perkins": ""
    "social_media_url": "",
    "platforms": {
        "ios": "8.0"
    "source": {
        "git": "",
        "tag": "1.1.4"
    "source_files": "ClingConstraints/Classes/*.{h,m,swift}",
    "swift_version": "4.2"

Pin It on Pinterest

Share This