Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Layout
Working directly with
NSLayoutConstraint
orNSLayoutAnchor
is pretty tedious. Material gives a simple API to create and update constraints easily.layout
code will addchildView
toparentView
and return instance ofLayout
:Layout
has completely chainable API to set constraints fluently. For example, to make achildView
have the constant height of32
, half of the width of its parent plus 20 point, 10 points belowanotherView
's bottom and in the horizontal center of its parent you just do:List of available methods
Layout
has following methods to set constraints in relation with parent and all takes optional offset argument:Methods for
width
andheight
There are 2 additional methods for setting constant height and width:
If you noticed there are 2 methods for
width
andheight
:The ones with
offset
are for constraining to parent and others to a constant. Following code would help to better distinguish them:Mehtods other than
width
andheight
does not takeoffset:
as named parameter take value, rather they are marked with_
just like constantwidth/height
:Anchors
To create constraints in relation to other view,
LayoutAnchor
can be used. All methods available for set constraints between child and parent are also available for anchoring onLayout
:Anchor of a view can be accessed via
view.anchor
:List of available anchors:
Safe Anchor
For pinning to the
safeAreaLayoutGuide
of a view, instead ofview.anchor
,view.safeAnchor
can be used. For below iOS 11,view.safeAnchor
will fall back to useview.anchor
.Omitting anchors
Anchors can be omitted if they are same as the anchor that's being pinned:
Same applies to
safeAnchor
as well:Compound constraints
Some constraints such as
edges
andcenter
are composed of multiple constraints. For exampleedges
creates 4 constraints. Those constrains can be used together with anchors:Priority and multiplier
Priority and multiplier of be set using
priority()
andmultiply()
methods respectively:Limitations
priority()
andmultiply()
methods can only be used to set last created constraint. For example,edges()
creates 4 constraints and the last one is right constraint, sopriority()
andmultiply()
will be effective on only it. You have to explicitly delareUpdating constraints
Layout
will update the constant of the constraint if it exists:Relation
Only
NSLayoutConstraint.Relation.equal
is supported right now,lessThanOrEqual
andgreaterThanOrEqual
is not supported yet.Some highlights of changes
Renaming:
centerHorizontally(offset:)
->centerX(offset:)
centerVertically(offset:)
->centerY(offset:)
horizontally(left:, right:)
->leftRight(left:, right:)
vertically(top:, bottom:)
->topBottom(top:, bottom:)
Removed:
size(_ size:)
horizontally(_ children:, left:, right:, interimSpace:)
vertically(_ children:, top:, bottom:, interimSpace:,
Sample app
Button.zip
See
ButtonViewController.swift
line36