Restraint alternatives and similar libraries
Based on the "Layout" category.
Alternatively, view Restraint alternatives based on common mentions on social networks and blogs.
-
Masonry
Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout -
PureLayout
The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. Objective-C and Swift compatible. -
MyLinearLayout
MyLayout is a powerful iOS UI framework implemented by Objective-C. It integrates the functions with Android Layout,iOS AutoLayout,SizeClass, HTML CSS float and flexbox and bootstrap. So you can use LinearLayout,RelativeLayout,FrameLayout,TableLayout,FlowLayout,FloatLayout,PathLayout,GridLayout,LayoutSizeClass to build your App 自动布局 UIView UITableView UICollectionView RTL -
PinLayout
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer] -
FlexLayout
FlexLayout adds a nice Swift interface to the highly optimized facebook/yoga flexbox implementation. Concise, intuitive & chainable syntax. -
Luminous
Luminous provides you a lot of information about the system and a lot of handy methods to quickly get useful data on the iOS platform. -
MisterFusion
MisterFusion is Swift DSL for AutoLayout. It is the extremely clear, but concise syntax, in addition, can be used in both Swift and Objective-C. Support Safe Area and Size Class. -
ManualLayout
✂ Easy to use and flexible library for manually laying out views and layers for iOS and tvOS. Supports AsyncDisplayKit. -
QuickLayout
Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code. -
MondrianLayout
🏗 A way to build AutoLayout rapidly than using InterfaceBuilder(XIB, Storyboard) in iOS. -
Framezilla
DISCONTINUED. Elegant library that wraps working with frames with a nice chaining syntax. -
BBLocationManager
A Location Manager for easily implementing location services & geofencing in iOS. Ready for iOS 11.
CodeRabbit: AI Code Reviews for Developers

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Restraint or a related project?
README
Restraint
Restraint is a very very small library to help make your use of NSLayoutConstraint
in Swift more legible & declarative.
- Like programmatic views?
- Like the benefits of using pure AutoLayout?
- Like clear and minimal interfaces?
- Dislike Visual Format Language and "stringly" typing?
- Dislike the verbosity of
NSLayoutConstraint
? - Dislike heavy dependencies?
- Practice
Restraint
!
Features
- [x] As simple as possible
- [x] Easy to maintain
- [x] Easy to replace
- [x] Easy to circumvent
- [x] Not too clever
- [x] Sane defaults
- [x] Automatic handling of
setTranslatesAutoresizingMaskIntoConstraints
Basic Example
Let's set the height & width of an imageView
to 200
points and center in the current UIView
. Here's how we would do that with NSLayoutConstraint
:
let imageViewWidthConstraint = NSLayoutConstraint(
item: imageView,
attribute: .Height,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 1.0,
constant: 200
)
let imageViewHeightConstraint = NSLayoutConstraint(
item: imageView,
attribute: .Width,
relatedBy: .Equal,
toItem: nil,
attribute: .NotAnAttribute,
multiplier: 1.0,
constant: 200
)
imageView.addConstraints([imageViewWidthConstraint, imageViewHeightConstraint])
let imageViewHorizontalConstraint = NSLayoutConstraint(
item: imageView,
attribute: .CenterX,
relatedBy: .Equal,
toItem: self,
attribute: .CenterX,
multiplier: 1.0,
constant: 0
)
let imageViewVerticalConstraint = NSLayoutConstraint(
item: imageView,
attribute: .CenterY,
relatedBy: .Equal,
toItem: self,
attribute: .CenterY,
multiplier: 1.0,
constant: 0
)
addConstraints([imageViewHorizontalConstraint, imageViewVerticalConstraint])
:fearful:
And of course, this depends upon your particular formatting conventions. You know how quickly this gets out of hand.
Here's how to apply these same constraints with Restraint
:
Restraint(imageView, .Width, .Equal, 200).addToView(imageView)
Restraint(imageView, .Height, .Equal, 200).addToView(imageView)
Restraint(imageView, .CenterX, .Equal, self, .CenterX).addToView(self)
Restraint(imageView, .CenterY, .Equal, self, .CenterY).addToView(self)
:massage:
Detailed Example
[Example View](Example.png)
For this view, we'd like to:
- Center the
imageView
(which is of a fixed size) vertically and horizontally in the containing view. - Center
topLabel
(which is of a variable size) vertically between the top ofimageView
and the top of the containing view. - Center
topLabel
horizontally in the containing view. - Center
bottomLabel
(which is of a variable size) vertically between the bottom ofimageView
and the bottom of the containing view. - Set the width of
bottomLabel
to the width of the containing view less its layout margins.
Here are those rules expressed with Restraint
:
// Image View Constraints
Restraint(imageView, .Width, .Equal, imageViewSize).addToView(self)
Restraint(imageView, .Height, .Equal, imageViewSize).addToView(self)
Restraint(imageView, .CenterX, .Equal, self, .CenterX).addToView(self)
Restraint(imageView, .CenterY, .Equal, self, .CenterY).addToView(self)
// Top Label Constraints
Restraint(topLabel, .CenterX, .Equal, imageView, .CenterX).addToView(self)
Restraint(topLabel, .CenterY, .Equal, imageView, .Top, 0.5, 0).addToView(self)
// Bottom Label Constraints
Restraint(bottomLabel, .Left, .Equal, self, .LeftMargin).addToView(self)
Restraint(bottomLabel, .Right, .Equal, self, .RightMargin).addToView(self)
Restraint(bottomLabel, .CenterY, .Equal, self, .CenterY, 1.5, (200 / 4)).addToView(self)
You can see this in action in [Example/View.swift](Example/Example/View.swift)
How It Works
Each Restraint
simply creates the appropriate NSLayoutConstraint
, and the call to addToView
disables translatesAutoresizingMaskIntoConstraints
on the left view in the constraint and adds that constraint to the target view.
Optionally, you can call Restraint().constraint()
to get an instance of NSLayoutConstraint
and add your constraint with UIView#addConstraint
or #addConstraints
later. For example:
let heightConstraint = Restraint(imageView, .Height, .Equal, 200).constraint()
imageView.addConstraint(heightConstraint)
In this case you'll need to handle disabling translatesAutoresizingMaskIntoConstraints
yourself.
Installation
Carthage
Add the following to your project's Cartfile
:
github "puffinsupply/Restraint" >= 1.0
Manual
Simply add [Restraint.swift](Restraint/Restraint.swift) to your project.
Contributors
License
*Note that all licence references and agreements mentioned in the Restraint README section above
are relevant to that project's source code only.