Latest1.0.1
Homepagehttps://github.com/Chris-Perkins/ClingConstraints
LicenseMIT
Platformsios 9.0
Authors

ClingConstraints

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)

ClingConstraintsClingConstraintsClingConstraints

Features

✅ 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

Installation

  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
        use_frameworks!
    
        pod 'ClingConstraints'
    end
  3. Run pod install in the podfile directory from your terminal
  4. Open up the .xcworkspace that CocoaPods created
  5. Done!

Examples

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:

activateAllConstraints()
deactivateAllConstraints()  

Example Project

ClingConstraints

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.

Documentation

Read the docs

Author

Chris Perkins

Thanks to Luis Padron for helping set this up! 👍

Latest podspec

{
    "name": "ClingConstraints",
    "version": "1.0.1",
    "summary": "Library for programmatic constraints in iOS; emphasis on readability",
    "description": "A programmatic constraints library for iOS written using Swift.nEasily write constraints in code and keep them readable!",
    "homepage": "https://github.com/Chris-Perkins/ClingConstraints",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Chris Perkins": "http://ChrisPerkins.me"
    },
    "social_media_url": "http://ChrisPerkins.me",
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/Chris-Perkins/ClingConstraints.git",
        "tag": "v1.0.1"
    },
    "source_files": [
        "ClingConstraints",
        "ClingConstraints/**/*.{h,m}"
    ],
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This