I am glad to share with you a lightweight Swift framework for Apple's Auto-Layout. It helps you write readable and compact UI code using simple API.
At first, it was only a few extenstions of UIView desiged for some of my commercial projects, but eventually code continued to grow, so I decided to move it to a seperate framework. I hope some of you find it helpful, so feel free to share your feedback and give some stars to EasySwiftLayout!
If you want to report bug or request new feature - open a ticket. I will try my best to cover them as soon as posible.
- iOS 9.0+
- Xcode 9.0+
- Swift 4+
All methods in EasySwiftLayout designed to be self-explaining, but at the same time all of them includes detail description of usage. You can check it both in code by pressing ⌥ key and clicking on the method, and in our API Cheat Sheet section down below.
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate EasySwiftLayout into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'EasySwiftLayout'Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate EasySwiftLayout into your Xcode project using Carthage, specify it in your Cartfile:
github "denandreychuk/EasySwiftLayout"
If you prefer not to use any of the aforementioned dependency managers, you can integrate EasySwiftLayout into your project manually.
-
Clone this repo by running:
$ git clone https://github.com/denandreychuk/EasySwiftLayout.git
-
Navigate to the project folder and open
EasySwiftLayout.xcodeproj. -
Select scheme
Build Frameworkby pressing^ + 0and run it on any device. It will automatically generate for youEasySwiftLayout.frameworkfile and open it inFinder. -
Move this file to your project folder.
-
Select your application in the Project Navigator (blue icon) to navigate to the target configuration window.
-
Select the desired target under the "Targets" heading in the sidebar.
-
In "General" tab click on the
+button under the "Embedded Binaries" section (in Xcode 11 it callsFrameworks, Libraries, and Embedded Content) and select movedEasySwiftLayout.frameworkfile. -
Done.
The
EasySwiftLayout.frameworkis automatically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
import EasySwiftLayout
class ViewController: UIViewController {
private let boxView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
addAndLayoutSubviews()
}
private func addAndLayoutSubviews() {
view.addSubview(boxView)
boxView.centerInSuperview()
boxView.size(toSquareWithSide: 50)
}
}width(_:)
Sets view's width to a specific size.
func width(_ size: CGFloat) -> Self-
Constraints width anchor using
NSLayoutConstraintto a specific size. -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
Pass non-zero size, otherwise this method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| size | CGFloat |
The size to set this view's width to. |
self with attribute @discardableResult.
height(_:)
Sets view's height to a specific size.
func height(_ size: CGFloat) -> Self-
Constraints width anchor using
NSLayoutConstraintto a specific size. -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
Pass non-zero size, otherwise this method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| size | CGFloat |
The size to set this view's height to. |
self with attribute @discardableResult.
size(_:)
Sets view's dimensions to a specific size.
func size(_ size: CGSize) -> Self-
Constraints width and height anchors using
NSLayoutConstraintto a specific size. -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
Pass non-zero size, otherwise this method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| size | CGSize |
The size to set this view's dimensions to. |
self with attribute @discardableResult.
size(toSquareWithSide:)
Sets view's dimensions to square with side.
func size(toSquareWithSide side: CGFloat) -> Self-
Constraints width and height anchors using
NSLayoutConstraintto match square size. -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
Pass non-zero side, otherwise this method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| side | CGFloat |
Square side to set this view's dimensions to. |
self with attribute @discardableResult.
pin(topTo:leftTo:bottomTo:rightTo:withInsets:)
Pins edges to the given NSLayoutAxisAnchors.
func pin(topTo top: NSLayoutYAxisAnchor? = nil, leftTo left: NSLayoutXAxisAnchor? = nil, bottomTo bottom: NSLayoutYAxisAnchor? = nil, rightTo right: NSLayoutXAxisAnchor? = nil, withInsets insets: UIEdgeInsets = .zero) -> Self-
Compact version of default Swift layout. Allows you to edges to specific
NSLayoutAxisAnchor. -
To make Auto-Layout works properly, it automatically sets view’s property
translatesAutoresizingMaskIntoConstraintstofalse
You should pass at least one anchor, otherwise this method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| top | NSLayoutYAxisAnchor |
Anchor to pin top to. |
| left | NSLayoutXAxisAnchor |
Anchor to pin left to. |
| bottom | NSLayoutYAxisAnchor |
Anchor to pin bottom to. |
| right | NSLayoutXAxisAnchor |
Anchor to pin right to. |
| insets | UIEdgeInsets |
Insets between edges. |
self with attribute @discardableResult.
pinEdge(_:toEdge:ofView:withInset:)
Pins edge to the given edge of another view with an inset.
func pinEdge(_ edge: ESLEdge, toEdge pinningEdge: ESLEdge, ofView anotherView: UIView, withInset inset: CGFloat = .zero) -> Self-
Consider, that you cannot pin edge to different axis, otherwise method will throw
fatalError(). X-axis constraints are not compatible with y-axis. -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
-
Another view must be in the same view hierarchy as this view.
-
Pin edges with same axis or method will throw
fatalError()
| Parameter | Type | Description |
|---|---|---|
| edge | ESLEdge |
The edge of this view to pin. |
| pinningEdge | ESLEdge |
The edge of another view to pin to. |
| anotherView | NSLayoutYAxisAnchor |
Another view to pin to. |
| inset | CGFloat |
Inset between edge of this view and edge of another view. |
self with attribute @discardableResult.
pinEdge(_:toSameEdgeOfView:withInset:)
Pins the given edge of the view to the corresponding margin of another view with an inset.
func pinEdge(_ edge: ESLEdge, toSameEdgeOfView anotherView: UIView, withInset inset: CGFloat = .zero) -> SelfTo make Auto-Layout works properly, it automatically sets view's property translatesAutoresizingMaskIntoConstraints to false
Another view must be in the same view hierarchy as this view.
| Parameter | Type | Description |
|---|---|---|
| edge | ESLEdge |
The edge of this view to pin. |
| anotherView | NSLayoutYAxisAnchor |
Another view to pin to. |
| inset | CGFloat |
Inset between edge of this view and edge of another view. |
self with attribute @discardableResult.
pinEdges(_:toSameEdgesOfView:withInsets:)
Pins the given edges of the view to the corresponding margins of another view with insets.
func pinEdges(_ edges: [ESLEdge] = ESLEdge.all, toSameEdgesOfView anotherView: UIView, withInsets insets: UIEdgeInsets = .zero) -> Self-
This method is intended to pin multiple edges, it is not recommended to use it for a single edge. For these purposes,
pinEdge(_:toSameEdgeOfView:withInset:)would be a better approach. -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
Another view must be in the same view hierarchy as this view.
| Parameter | Type | Description |
|---|---|---|
| edges | [ESLEdge] |
The edges of this view to pin. |
| anotherView | NSLayoutYAxisAnchor |
Another view to pin to. |
| insets | UIEdgeInsets |
Insets between edges of this view and corresponding edges of another view |
self with attribute @discardableResult.
pinEdges(_:toSameEdgesOfView:withInset:)
Pins the given edges of the view to the corresponding margins of another view with equal inset.
func pinEdges(_ edges: [ESLEdge] = ESLEdge.all, toSameEdgesOfView anotherView: UIView, withInset inset: CGFloat) -> Self-
This method is intended to pin multiple edges, it is not recommended to use it for a single one. For these purposes,
pinEdge(_:toSameEdgeOfView:withInset:)would be a better approach. -
If you want to customize inset based on edge, use
pinEdges(_:toSameEdgesOfView:withInsets:). -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
Another view must be in the same view hierarchy as this view.
| Parameter | Type | Description |
|---|---|---|
| edges | [ESLEdge] |
The edges of this view to pin. |
| anotherView | NSLayoutYAxisAnchor |
Another view to pin to. |
| insets | CGFloat |
Inset between edges of this view and corresponding edges of another view |
self with attribute @discardableResult.
Just to remind, let's say you have a view called MyView, which has a UIButton(loginButton) over it. In this case, MyView is a superview for loginButton and loginButton is a subview of MyView.
pinEdge(_:toSuperviewEdge:withInset:)
Pins edge to the given edge of its superview with an inset.
func pinEdge(_ edge: ESLEdge, toSuperviewEdge superviewEdge: ESLEdge, withInset inset: CGFloat = .zero) -> Self-
Use this method only if you want to pin view's edge to opposite margin of its superview, in other cases `pinEdgeToSuperview(_:withInset:) would be a better approach.
-
Consider, that you cannot pin edge to different axis, otherwise method will throw
fatalError(). X-axis constraints are not compatible with y-axis. -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
-
View should have superview, otherwise method will have no effect.
-
Pin edges with same axis or method will throw
fatalError()
| Parameter | Type | Description |
|---|---|---|
| edge | ESLEdge |
The edge of this view to pin. |
| superviewEdge | ESLEdge |
The edge of its superview to pin to. |
| inset | CGFloat |
Inset from the superview's bound |
self with attribute @discardableResult.
pinEdgeToSuperview(_:withInset:)
Pins the given edge of the view to the corresponding margin of its superview with an inset.
func pinEdgeToSuperview(_ edge: ESLEdge, withInset inset: CGFloat = .zero) -> Self- To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
View should have superview, otherwise method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| edge | ESLEdge |
The edge of this view to pin to the corresponding margin. |
| inset | CGFloat |
Inset from the superview's bound |
self with attribute @discardableResult.
pinEdgesToSuperview(_:withInsets:)
Pins the given edges of the view to the corresponding margins of its superview with an inset.
func pinEdgesToSuperview(_ edges: [ESLEdge] = ESLEdge.all, withInsets insets: UIEdgeInsets = .zero) -> Self-
This method is intended to pin multiple edges, it is not recommended to use it for a single edge. For these purposes,
pinEdgeToSuperview(_:withInset:)would be a better approach. -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
View should have superview, otherwise method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| edges | [ESLEdge] |
The edges of this view to pin to the corresponding margins. |
| insets | UIEdgeInsets |
Insets from the superview's bounds. |
self with attribute @discardableResult.
pinEdgesToSuperview(_:withInset:)
Pins the given edges of the view to the corresponding margins of its superview with equal inset.
func pinEdgesToSuperview(_ edges: [ESLEdge] = ESLEdge.all, withInset inset: CGFloat) -> Self-
This method is intended to pin multiple edges, it is not recommended to use it for a single edge. For these purposes,
pinEdgeToSuperview(_:withInset:)would be a better approach. -
If you want to customize inset based on edge, use
pinEdgesToSuperview(_:withInsets:). -
To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
View should have superview, otherwise method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| edges | [ESLEdge] |
The edges of this view to pin to the corresponding margins. |
| inset | CGFloat |
Inset from superview's bounds. |
self with attribute @discardableResult.
pinHorizontalEdgesToSuperview(withInset:)
Pins left and right to the corresponding margins of its superview with equal inset.
func pinHorizontalEdgesToSuperview(withInset inset: CGFloat = .zero) -> Self- Helper method. Have the same benefits and requirement as
pinEdgesToSuperview(_:withInset:). - To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
View should have superview, otherwise method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| inset | CGFloat |
Inset from superview's bounds. |
self with attribute @discardableResult.
pinVerticalEdgesToSuperview(withInset:)
Pins top and bottom to the corresponding margins of its superview with equal inset.
func pinVerticalEdgesToSuperview(withInset inset: CGFloat = .zero) -> Self- Helper method. Have the same benefits and requirement as
pinEdgesToSuperview(_:withInset:). - To make Auto-Layout works properly, it automatically sets view's property
translatesAutoresizingMaskIntoConstraintstofalse
View should have superview, otherwise method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| inset | CGFloat |
Inset from superview's bounds. |
self with attribute @discardableResult.
centerInView(_:withOffset:)
Centers the view in the given view with offset.
func centerInView(_ anotherView: UIView, withOffset offset: ESLOffset = .zero) -> SelfTo make Auto-Layout works properly, it automatically sets view's property translatesAutoresizingMaskIntoConstraints to false
Another view must be in the same view hierarchy as this view.
| Parameter | Type | Description |
|---|---|---|
| anotherView | UIView |
View to center in. |
| offset | ESLOffset |
Axis offset. |
self with attribute @discardableResult.
centerInView(_:axis:withOffset:)
Centers the given axis of the view in another view with offset.
func centerInView(_ anotherView: UIView, axis: ESLAxis, withOffset offset: CGFloat = .zero) -> SelfTo make Auto-Layout works properly, it automatically sets view's property translatesAutoresizingMaskIntoConstraints to false
Another view must be in the same view hierarchy as this view.
| Parameter | Type | Description |
|---|---|---|
| anotherView | UIView |
View to center in. |
| axis | ESLAxis |
Axis to center |
| offset | ESLOffset |
Axis offset. |
self with attribute @discardableResult.
centerInSuperview(_:withOffset:)
Centers the given axis of the view in its superview with offset.
func centerInSuperview(_ axis: ESLAxis, withOffset offset: CGFloat = .zero) -> SelfTo make Auto-Layout works properly, it automatically sets view's property translatesAutoresizingMaskIntoConstraints to false
View should have superview, otherwise this method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| axis | ESLAxis |
Axis to center |
| offset | CGFloat |
Axis offset. |
self with attribute @discardableResult.
centerInSuperview(withOffset:)
Centers the view in its superview with offset.
func centerInSuperview(withOffset offset: ESLOffset = .zero) -> SelfTo make Auto-Layout works properly, it automatically sets view's property translatesAutoresizingMaskIntoConstraints to false
View should have superview, otherwise this method will have no effect.
| Parameter | Type | Description |
|---|---|---|
| offset | ESLOffset |
Axis offset. |
self with attribute @discardableResult.
EasySwiftLayout is released under the MIT license. See LICENSE for details.
