TieKit is a Swift DSL framework designed to ease the use of NSLayoutAnchor
and NSLayoutconstraint
in UIKit.
let view = UIView(frame: .zero)
_ = view.top() // top anchor
_ = view.top(margin: false) // top anchor of layout margin guide
_ = view.top(margin: true) // top anchor
// tie anchors with equal relation
let constraint: TieConstraint = subview.top() --- superview.top()
// tie anchors with constant spacing, and ignoring result
subview.leading() --- superview.leading() + 20
// tie anchors with 'greater than or equal' relation
let constraint: TieConstraint = subview.top() ~~> superview.top()
// tie anchors with constant spacing, and ignoring result
subview.leading() ~~> superview.leading() + 20
// tie anchors with 'less than or equal' relation
let constraint: TieConstraint = subview.top() <~~ superview.top()
// tie anchors with constant spacing, and ignoring result
subview.leading() <~~ superview.leading() + 20
// dimension equal to constant
let constraint: TieConstraint = subview.height() --= 80
// dimension greater or equal to constant
let constraint: TieConstraint = subview.height() ~>= 80
// dimension less or equal to constant and ignoring result
subview.height() <~= 80
The TieSize
enumeration set predefined sizes you can use in your code or in your storyboards.
The base value TieSize.spacing
defines the base spacing. You can change it (as soon as possible in your app lifecycle, like your AppDelegate).
TieSize.spacing = 6
let smallSpacing: CGFloat = TieSize.s.spacing
You can use TieSize
to add to a constraint directly.
subview.leading() --- superview.leading() + .s
When setting a constraint in storyboard you can use TieSize
to.
- Set the class of the constraint to
TieConstraint
. - In the Attributes inspector tab, set the
Spacing name
value as a text with the predefined values:xs
,s
,m
,l
,xl
,xxl
. You can even sum the values:m+s
.