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
-
Install CocoaPods
-
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
-
Run
pod install
in the podfile directory from your terminal -
Open up the
.xcworkspace
that CocoaPods created -
Done!
// Creates and activates a constraint that makes thisView's height equal to thatView's
thisView.copy(.height, of: thatView)
// thisView copies thatView's height * 0.5 - 30.
thisView.copy(.height, of: thatView).withOffset(-30).withMultiplier(0.5)
// thisView copies the right, top, and left anchor constraints of that view-- in one line.
thisView.copy(.right, .top, .left, of: thatView)
// thisView positions itself to the right of thatView with a spacing of 5
thisView.cling(.left, to: thatView, .right).withOffset(5)
// Fills a view from the top to the bottom with the given views
thisView.fill(.topToBottom, withViews: [thatView1, thatView2], spacing: 0)
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)
On any NSLayoutConstraint:
withMultiplier(_: CGFloat)
withOffset(_: CGFloat)
withPriority(_: UILayoutPriority)
withRelation(_: NSLayoutRelation)
In any collection of constraints:
activateAllConstraints()
deactivateAllConstraints()
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
Thanks to Luis Padron for helping set this up! 👍