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"
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler. It is in early development, but EasySwiftLayout does support its use on supported platforms.
Once you have your Swift package set up, adding EasySwiftLayout as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/Pimine/EasySwiftLayout.git", .upToNextMajor(from: "1.6.0"))
]
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 Framework
by pressing^ + 0
and run it on any device. It will automatically generate for youEasySwiftLayout.framework
file 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.framework
file. -
Done.
The
EasySwiftLayout.framework
is 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() {
boxView
.add(toSuperview: view)
.centerInSuperview()
.size(toSquareWithSide: 50)
}
}
EasySwiftLayout comes with project example. You can check out how easily you can create screens like this with ESL:
- Navigate to the project folder and open
EasySwiftLayout.xcworkspace
. - Select scheme
iOS Example
by pressing^ + 0
and just run it on any device.
add(toSuperview:)
Adds the view as a subview of specified superview.
func add(toSuperview superview: UIView) -> Self
Parameter | Type | Description |
---|---|---|
superview | UIView |
The superview to which add subview. |
self
with attribute @discardableResult
.
width(_:usingRelation:priority:)
Sets the width of the view using the specified type of relation to the given size with the priority of the constraint.
func width(_ width: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Constraints the width anchor using
NSLayoutConstraint
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
Pass size greater than zero, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
width | CGFloat |
The value to set this view width to. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
height(_:usingRelation:priority:)
Sets the height of the view using the specified type of relation to the given size with the priority of the constraint.
func height(_ height: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Constraints the height anchor using
NSLayoutConstraint
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
Pass size greater than zero, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
height | CGFloat |
The value to set this view height to. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
size(_:usingRelation:priority:)
Sets the dimensions of the view using the specified type of relation to the given size with the priority of the constraint.
func size(_ size: CGSize, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Constraints the height and width anchors using
NSLayoutConstraint
-
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
Pass size greater than zero, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
size | CGSize |
The size to set this view dimensions to. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
size(toSquareWithSide:usingRelation:priority:)
Sets the dimensions of the view to a square with the side using the specified type of relation to the given size with the priority of the constraint.
func size(toSquareWithSide side: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Constraints width and height anchors using
NSLayoutConstraint
to match square size. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
Pass side greater than zero, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
side | CGFloat |
Square side to set this view dimensions to. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
width(match:withInset:usingRelation:priority:)
Sets the width of the view using the specified type of relation to the width of another view with the inset and priority of the constraint.
func width(match anotherView: UIView, withInset inset: CGFloat = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Constraints width and height anchors using
NSLayoutConstraint
to match square size. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
Parameter | Type | Description |
---|---|---|
anotherView | UIView |
Another view to set this view width to. |
inset | CGFloat |
The value to inset (or shrunk) the width. Negative value cause the width to be outset (or expanded). |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
height(match:withInset:usingRelation:priority:)
Sets the height of the view using the specified type of relation to the height of another view with the inset and priority of the constraint.
func height(match anotherView: UIView, withInset inset: CGFloat = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Constraints width and height anchors using
NSLayoutConstraint
to match square size. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
Parameter | Type | Description |
---|---|---|
anotherView | UIView |
Another view to set this view height to. |
inset | CGFloat |
The value to inset (or shrunk) the height. Negative value cause the height to be outset (or expanded). |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
size(match:withInsets:usingRelation:priority:)
Sets the size of the view using the specified type of relation to the size of another view with the insets and priority of the constraints.
func size(match anotherView: UIView, withInsets insets: ESLSizeInsets = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Constraints width and height anchors using
NSLayoutConstraint
to match square size. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
Parameter | Type | Description |
---|---|---|
anotherView | UIView |
Another view to set this view height to. |
insets | ESLSizeInsets |
The values to inset (or shrunk) the size. Negative values cause the size to be outset (or expanded). |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
size(match:withInset:usingRelation:priority:)
Sets the size of the view using the specified type of relation to the size of another view with the insets and priority of the constraints.
func size(match anotherView: UIView, withInset inset: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Constraints width and height anchors using
NSLayoutConstraint
to match square size. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
Parameter | Type | Description |
---|---|---|
anotherView | UIView |
Another view to set this view height to. |
inset | CGFloat |
The value to inset (or shrunk) the size. Negative value cause the size to be outset (or expanded). |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pin(topTo:leftTo:bottomTo:rightTo:withInsets:priority:)
Pins the edges to the given NSLayoutAxisAnchors with the insets and priority of the constraints.
func pin(topTo top: NSLayoutYAxisAnchor? = nil, leftTo left: NSLayoutXAxisAnchor? = nil, bottomTo bottom: NSLayoutYAxisAnchor? = nil, rightTo right: NSLayoutXAxisAnchor? = nil, withInsets insets: UIEdgeInsets = .zero, priority: UILayoutPriority = .required) -> Self
-
Compact version of default Swift layout. Allows you to pin edges to specific
NSLayoutAxisAnchor
. -
To make Auto-Layout works properly, it automatically sets viewβs property
translatesAutoresizingMaskIntoConstraints
tofalse
You should pass at least one anchor, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
top | NSLayoutYAxisAnchor |
The anchor to pin top to. |
left | NSLayoutXAxisAnchor |
The anchor to pin left to. |
bottom | NSLayoutYAxisAnchor |
The anchor to pin bottom to. |
right | NSLayoutXAxisAnchor |
The anchor to pin right to. |
insets | UIEdgeInsets |
The insets between the edges. |
priority | UILayoutPriority |
The priority of the constraints. |
self
with attribute @discardableResult
.
pinEdge(_:toEdge:ofView:withInset:usingRelation:priority:)
Pins the edge of the view using the specified type of relation to the given edge of another view with the inset and priority of the constraint.
func pinEdge(_ edge: ESLEdge, toEdge pinningEdge: ESLEdge, ofView anotherView: UIView, withInset inset: CGFloat = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Consider, accordingly to Apple's documentation, you cannot pin edges with different axis, otherwise it will throw fatal error.
-
To make Auto-Layout works properly, it automatically sets viewβs property
translatesAutoresizingMaskIntoConstraints
tofalse
.
-
Another view must be in the same view hierarchy as this view.
-
Pin edges with same axis or method will throw fatal error.
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 |
The inset between the edge of this view and the edge of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdge(_:toSameEdgeOfView:withInset:usingRelation:priority:)
Pins the given edge of the view using the specified type of relation to the corresponding margin of another view with the inset and priority of the constraint.
func pinEdge(_ edge: ESLEdge, toSameEdgeOfView anotherView: UIView, withInset inset: CGFloat = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
To 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 |
The inset beetween the edge of this view and the corresponding edge of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(_:toSameEdgesOfView:withInsets:usingRelation:priority:)
Pins the given edges of the view using the specified type of relation to the corresponding margins of another view with the insets and priority of the constraints.
func pinEdges(_ edges: [ESLEdge] = ESLEdge.all, toSameEdgesOfView anotherView: UIView, withInsets insets: UIEdgeInsets = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
If you need to customize the insets based on the edge, use
pinEdges(_:toSameEdgesOfView:withInset:usingRelation:priority:)
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
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 |
The insets beetween the edges of this view and corresponding edges of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(_:toSameEdgesOfView:withInset:usingRelation:priority:)
Pins the given edges of the view using the specified type of relation to the corresponding margins of another view with the equal insets and priority of the constraints.
func pinEdges(_ edges: [ESLEdge] = ESLEdge.all, toSameEdgesOfView anotherView: UIView, withInset inset: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
If you donβt need to customize the insets based on the edge, use
pinEdges(_:toSameEdgesOfView:withInsets:usingRelation:priority:)
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
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 |
The inset beetween the edges of this view and corresponding edges of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(ofGroup:toSameEdgesOfView:withInset:usingRelation:priority:)
Pins edges of the view of the given group using the specified type of relation to the corresponding margins of another view with the equal insets and priority of the constraints.
func pinEdges(ofGroup edgeGroup: ESLEdgeGroup, toSameEdgesOfView anotherView: UIView, withInset inset: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
To make Auto-Layout works properly, it automatically sets view property translatesAutoresizingMaskIntoConstraints
to false
.
Another view must be in the same view hierarchy as this view.
Parameter | Type | Description |
---|---|---|
edgeGroup | ESLEdgeGroup |
The group of edges of this view to pin to. |
anotherView | NSLayoutYAxisAnchor |
Another view to pin to. |
insets | CGFloat |
The inset beetween the edges of this view and corresponding edges of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(toSameEdgesOfView:excludingEdge:withInsets:usingRelation:priority:)
Pins the edges of the view using the specified type of relation to the corresponding margins of another view with the insets and priority of the constraints, excluding one edge
func pinEdges(toSameEdgesOfView anotherView: UIView, excludingEdge excludedEdge: ESLEdge, withInsets insets: UIEdgeInsets = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
- If you donβt need to customize the insets based on the edge, use
pinEdges(toSameEdgesOfView:excludingEdge:withInset:usingRelation:priority:)
. - To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
Another view must be in the same view hierarchy as this view.
Parameter | Type | Description |
---|---|---|
anotherView | NSLayoutYAxisAnchor |
Another view to pin to. |
excludedEdge | ESLEdge |
The edge to be ingored and not pinned. |
insets | UIEdgeInsets |
The insets beetween the edges of this view and corresponding edges of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(toSameEdgesOfView:excludingEdge:withInset:usingRelation:priority:)
Pins the edges of the view using the specified type of relation to the corresponding margins of another view with the equal inset and priority of the constraints, excluding one edge.
func pinEdges(toSameEdgesOfView anotherView: UIView, excludingEdge excludedEdge: ESLEdge, withInset inset: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
- If you need to customize the insets based on the edge, use
pinEdges(toSameEdgesOfView:excludingEdge:withInsets:usingRelation:priority:)
. - To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
Another view must be in the same view hierarchy as this view.
Parameter | Type | Description |
---|---|---|
anotherView | NSLayoutYAxisAnchor |
Another view to pin to. |
excludedEdge | ESLEdge |
The edge to be ingored and not pinned. |
inset | CGFloat |
The inset beetween the edges of this view and corresponding edges of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdge(_:toEdge:ofGuide:withInset:usingRelation:priority:)
Pins the edge of the view using the specified type of relation to the given edge of guide with the inset and priority of the constraint.
func pinEdge(_ edge: ESLEdge, toEdge pinningEdge: ESLEdge, ofGuide guide: ESLGuide, withInset inset: CGFloat = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Consider, accordingly to Apple's documentation, you cannot pin edges with different axis, otherwise it will throw fatal error.
-
To make Auto-Layout works properly, it automatically sets viewβs property
translatesAutoresizingMaskIntoConstraints
tofalse
.
Pin edges with same axis or method will throw fatal error.
Parameter | Type | Description |
---|---|---|
edge | ESLEdge |
The edge of this view to pin. |
pinningEdge | ESLEdge |
The edge of another view to pin to. |
guide | ESLGuide |
The guide to pin to.. |
inset | CGFloat |
The inset between the edge of this view and the edge of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdge(_:toSameEdgeOfGuide:withInset:usingRelation:priority:)
Pins the given edge of the view using the specified type of relation to the corresponding margin of guide with the inset and priority of the constraint.
func pinEdge(_ edge: ESLEdge, toSameEdgeOfGuide guide: ESLGuide, withInset inset: CGFloat = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
To make Auto-Layout works properly, it automatically sets viewβs property translatesAutoresizingMaskIntoConstraints
to false
.
Parameter | Type | Description |
---|---|---|
edge | ESLEdge |
The edge of this view to pin. |
guide | ESLGuide |
The guide to pin to. |
inset | CGFloat |
The inset beetween the edge of this view and the corresponding edge of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(_:toSameEdgesOfGuide:withInsets:usingRelation:priority:)
Pins the given edges of the view using the specified type of relation to the corresponding margins of guide with the insets and priority of the constraints.
func pinEdges(_ edges: [ESLEdge] = ESLEdge.all, toSameEdgesOfGuide guide: ESLGuide, withInsets insets: UIEdgeInsets = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
If you need to customize the insets based on the edge, use
pinEdges(_:toSameEdgesOfGuide:withInset:usingRelation:priority:)
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
Parameter | Type | Description |
---|---|---|
edges | [ESLEdge] |
The edges of this view to pin. |
guide | ESLGuide |
The guide to pin to.. |
insets | UIEdgeInsets |
The insets beetween the edges of this view and corresponding edges of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(_:toSameEdgesOfGuide:withInset:usingRelation:priority:)
Pins the given edges of the view using the specified type of relation to the corresponding margins of guide with the equal insets and priority of the constraints.
func pinEdges(_ edges: [ESLEdge] = ESLEdge.all, toSameEdgesOfGuide guide: ESLGuide, withInset inset: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
If you donβt need to customize the insets based on the edge, use
pinEdges(_:toSameEdgesOfGuide:withInsets:usingRelation:priority:)
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
Parameter | Type | Description |
---|---|---|
edges | [ESLEdge] |
The edges of this view to pin. |
guide | ESLGuide |
The guide to pin to. |
insets | CGFloat |
The inset beetween the edges of this view and corresponding edges of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(ofGroup:toSameEdgesOfGuide:withInset:usingRelation:priority:)
Pins edges of the view of the given group using the specified type of relation to the corresponding margins of guide with the equal insets and priority of the constraints.
func pinEdges(ofGroup edgeGroup: ESLEdgeGroup, toSameEdgesOfGuide guide: ESLGuide, withInset inset: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
To make Auto-Layout works properly, it automatically sets view property translatesAutoresizingMaskIntoConstraints
to false
.
Parameter | Type | Description |
---|---|---|
edgeGroup | ESLEdgeGroup |
The group of edges of this view to pin to. |
guide | ESLGuide |
The guide to pin to. |
insets | CGFloat |
The inset beetween the edges of this view and corresponding edges of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(toSameEdgesOfGuide:excludingEdge:withInsets:usingRelation:priority:)
Pins the edges of the view using the specified type of relation to the corresponding margins of guide with the insets and priority of the constraints, excluding one edge.
func pinEdges(toSameEdgesOfGuide guide: ESLGuide, excludingEdge excludedEdge: ESLEdge, withInsets insets: UIEdgeInsets = .zero, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
- If you donβt need to customize the insets based on the edge, use
pinEdges(toSameEdgesOfGuide:excludingEdge:withInset:usingRelation:priority:)
. - To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
Parameter | Type | Description |
---|---|---|
guide | ESLGuide |
The guide to pin to. |
excludedEdge | ESLEdge |
The edge to be ingored and not pinned. |
insets | UIEdgeInsets |
The insets beetween the edges of this view and corresponding edges of another view. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdges(toSameEdgesOfGuide:excludingEdge:withInset:usingRelation:priority:)
Pins the edges of the view using the specified type of relation to the corresponding margins of guide with the equal inset and priority of the constraints, excluding one edge.
func pinEdges(toSameEdgesOfGuide guide: ESLGuide, excludingEdge excludedEdge: ESLEdge, withInset inset: CGFloat, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
- If you need to customize the inset based on the edge, use
pinEdges(toSameEdgesOfGuide:excludingEdge:withInsets:usingRelation:priority:)
. - To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
Parameter | Type | Description |
---|---|---|
guide | ESLGuide |
The guide to pin to. |
excludedEdge | ESLEdge |
The edge to be ingored and not pinned. |
inset | CGFloat |
The inset beetween the edges of this view and corresponding edges of guide. |
relation | NSLayoutConstraint.Relation |
The type of relationship for the constraints. |
priority | UILayoutPriority |
The priority of the constraint. |
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:respectingGuide:usingRelation:priority:)
Pins the edge of the view using the specified type of relation to the given edge of its superview with the inset and priority of the constraint. Optionally respects one of pre-defined Apple's layout guides.
func pinEdge(_ edge: ESLEdge, toSuperviewEdge superviewEdge: ESLEdge, withInset inset: CGFloat = .zero, respectingGuide guide: ESLSuperviewGuide = .none, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
Consider, accordingly to Apple's documentation, you cannot pin edges with different axis, otherwise it will throw fatal error.
-
Use this method only if you want to pin the edge of the view to the opposite margin of its superview, in other cases
pinEdgeToSuperview(_:withInset:usingRelation:priority:)
would be a better approach. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
-
The view should have the superview, otherwise method will have no effect.
-
Pin edges with same axis or method will throw fatal error.
Parameter | Type | Description |
---|---|---|
edge | ESLEdge |
The edge of this view to pin. |
superviewEdge | ESLEdge |
The edge of its superview to pin to. |
inset | CGFloat |
The inset between the edge of this view and the edge of its superview. |
guide | ESLSuperviewGuide |
The guide to respect in layout. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdgeToSuperview(_:withInset:respectingGuide:usingRelation:priority:)
Pins the given edge of the view using the specified type of relation to the corresponding margin of its superview with the inset and priority of the constraint. Optionally respects one of pre-defined Apple's layout guides.
func pinEdgeToSuperview(_ edge: ESLEdge, withInset inset: CGFloat = .zero, respectingGuide guide: ESLSuperviewGuide = .none, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
To make Auto-Layout works properly, it automatically sets view property translatesAutoresizingMaskIntoConstraints
to false
.
The view should have the superview, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
edge | ESLEdge |
The edge of this view to pin. |
inset | CGFloat |
The inset beetween the edge of this view and the corresponding edge of its superview. |
guide | ESLSuperviewGuide |
The guide to respect in layout. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdgesToSuperview(_:withInsets:respectingGuide:usingRelation:priority:)
Pins the given edges of the view using the specified type of relation to the corresponding margins of its superview with the insets and priority of the constraints. Optionally respects one of pre-defined Apple's layout guides.
func pinEdgesToSuperview(_ edges: [ESLEdge] = ESLEdge.all, withInsets insets: UIEdgeInsets = .zero, respectingGuide guide: ESLSuperviewGuide = .none, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
If you don't need to customize the insets based on the edge, use
pinEdgesToSuperview(_:withInset:usingRelation:priority:)
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
The view should have the superview, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
edges | [ESLEdge] |
The edges of this view to pin. |
insets | UIEdgeInsets |
The insets beetween the edges of this view and the corresponding edges of its superview. |
guide | ESLSuperviewGuide |
The guide to respect in layout. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdgesToSuperview(_:withInset:respectingGuide:usingRelation:priority:)
Pins the given edges of the view using the specified type of relation to the corresponding margins of its superview with the equal insets and priority of the constraints. Optionally respects one of pre-defined Apple's layout guides.
func pinEdgesToSuperview(_ edges: [ESLEdge] = ESLEdge.all, withInset inset: CGFloat, respectingGuide guide: ESLSuperviewGuide = .none, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
If you need to customize the insets based on the edge, use
pinEdgesToSuperview(_:withInsets:usingRelation:priority:)
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
The view should have the superview, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
edges | [ESLEdge] |
The edges of this view to pin. |
inset | CGFloat |
The inset beetween the edges of this view and the orresponding edges of its superview. |
guide | ESLSuperviewGuide |
The guide to respect in layout. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdgesToSuperview(ofGroup:withInset:respectingGuide:usingRelation:priority:)
Pins edges of the view of the given group using the specified type of relation to the corresponding margins of its superview with the equal insets and priority of the constraints. Optionally respects one of pre-defined Apple's layout guides.
func pinEdgesToSuperview(ofGroup group: ESLEdgeGroup, withInset inset: CGFloat = .zero, respectingGuide guide: ESLSuperviewGuide = .none, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
To make Auto-Layout works properly, it automatically sets view property translatesAutoresizingMaskIntoConstraints
to false
The view should have the superview, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
group | ESLEdgeGroup |
The group of edges of this view to pin to. |
inset | CGFloat |
The inset beetween the edges of this view and the orresponding edges of its superview. |
guide | ESLSuperviewGuide |
The guide to respect in layout. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdgesToSuperview(excludingEdge:withInsets:respectingGuide:usingRelation:priority:)
Pins the edges of the view using the specified type of relation to the corresponding margins of its superview with the insets and priority of the constraints, excluding one edge. Optionally respects one of pre-defined Apple's layout guides.
func pinEdgesToSuperview(excludingEdge excludedEdge: ESLEdge, withInsets insets: UIEdgeInsets = .zero, respectingGuide guide: ESLSuperviewGuide = .none, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
If you don't need to customize the insets based on the edge, use
pinEdgesToSuperview(excludingEdge:withInset:usingRelation:priority:)
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
The view should have the superview, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
excludedEdge | ESLEdge |
The edge to be ingored and not pinned. |
insets | UIEdgeInsets |
The insets beetween the edges of this view and the orresponding edges of its superview. |
guide | ESLSuperviewGuide |
The guide to respect in layout. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
pinEdgesToSuperview(excludingEdge:withInset:respectingGuide:usingRelation:priority:)
Pins the edges of the view using the specified type of relation to the corresponding margins of its superview with the equal inset and priority of the constraints, excluding one edge. Optionally respects one of pre-defined Apple's layout guides.
func pinEdgesToSuperview(excludingEdge excludedEdge: ESLEdge, withInset inset: CGFloat, respectingGuide guide: ESLSuperviewGuide = .none, usingRelation relation: NSLayoutConstraint.Relation = .equal, priority: UILayoutPriority = .required) -> Self
-
If you need to customize the insets based on the edge, use
pinEdgesToSuperview(excludingEdge:withInset:usingRelation:priority:)
. -
To make Auto-Layout works properly, it automatically sets view property
translatesAutoresizingMaskIntoConstraints
tofalse
.
The view should have the superview, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
excludedEdge | ESLEdge |
The edge to be ingored and not pinned. |
inset | CGFloat |
The inset beetween the edges of this view and the orresponding edges of its superview. |
guide | ESLSuperviewGuide |
The guide to respect in layout. |
relation | NSLayoutConstraint.Relation |
The type of relationship for constraint. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
centerInView(_:withOffset:priority:)
Centers the view in another view with the offset and priority of the constraint.
func centerInView(_ anotherView: UIView, withOffset offset: UIOffset = .zero, priority: UILayoutPriority = .required) -> Self
To make Auto-Layout works properly, it automatically sets view property translatesAutoresizingMaskIntoConstraints
to false
Another view must be in the same view hierarchy as this view.
Parameter | Type | Description |
---|---|---|
anotherView | UIView |
Another view to center in. |
offset | UIOffset |
Axis offset. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
centerInView(_:axis:withOffset:priority:)
Centers the axis of this view in another view with the offset and priority of the constraint.
func centerInView(_ anotherView: UIView, axis: ESLAxis, withOffset offset: CGFloat = .zero, priority: UILayoutPriority = .required) -> Self
To make Auto-Layout works properly, it automatically sets view 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 | UIOffset |
Axis offset. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
centerInSuperview(withOffset:priority:)
Centers the view in its superview view with the offset and priority of the constraint.
func centerInSuperview(withOffset offset: UIOffset = .zero, priority: UILayoutPriority = .required) -> Self
To make Auto-Layout works properly, it automatically sets view property translatesAutoresizingMaskIntoConstraints
to false
The view should have the superview, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
offset | UIOffset |
Axis offset. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
centerInSuperview(axis:withOffset:priority:)
Centers the axis of this view in its superview with the offset and priority of the constraint.
func centerInSuperview(axis: ESLAxis, withOffset offset: CGFloat = .zero, priority: UILayoutPriority = .required) -> Self
To make Auto-Layout works properly, it automatically sets view property translatesAutoresizingMaskIntoConstraints
to false
The view should have the superview, otherwise this method will have no effect.
Parameter | Type | Description |
---|---|---|
axis | ESLAxis |
Axis to center. |
offset | `CGFloat | Axis offset. |
priority | UILayoutPriority |
The priority of the constraint. |
self
with attribute @discardableResult
.
EasySwiftLayout is released under the MIT license. See LICENSE for details.