/ClingConstraints

Yet another library for programmatic constraints in iOS; emphasis on readability.

Primary LanguageSwiftMIT LicenseMIT

Version License Platform

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)

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

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! 👍