Currently, property chaining is supported in the following types:
UIView, UIControl, UILabel, UIImageView, UIScrollView, UITextView, UITableView, UICollectionView, UITextField, UIButton, UISlider, UISwitch, UIStackView.
You can also easily generate functions for other types – see how
😋 And some extra syntactic sugar:
Assignable to variable:
class ViewController: UIViewController {
weak var myLabel: UILabel!
override func loadView() {
...
view.addSubview(
UILabel()
.numberOfLines(0)
.text("Voila")
// sets a reference of object that calls function(in this case, created UILabel instance) to passed variable
.assign(to: &myLabel)
)
...
}
}
Closure based actions and gestures:
UIControl()
.addAction(for: .valueChanged, { print("value changed") })
UIButton()
.title("Tap me")
.onTap({ print("didTap") }) // wrap .addAction(for: .touchUpInside, { .. })
UIView()
.onTapGesture({ print("Kek") })
.onLongTapGesture({ print("Cheburek") })
// ⚠️ Don't forget about ARC when use some parent view in action closure, to prevent retain cycle
You can set constraints using the same chainable style with anchor constraint.
The return type of builder will be AutoLayoutItem – a simple storage of constants instructions. To build and activate them, just call activate() function.
ℹ️ NOTE: if you add constraint-builders to a UIView (i.e. don’t use anchor-chaining before call add(...)), constraints activations will occur immediately after adding in superview. In other words, the return type of the activate() function will be Self (i.e. UIView)
let profileView = UIView()
.backgroundColor(.gray)
.add({
UIImageView()
.assign(to: &avatarView)
.contentMode(.scaleAspectFit)
.sizeAnchor(40)
.leftAnchor(16)
.verticalAnchor(0)
UILabel()
.numberOfLines(2)
.rightAnchor(0)
.leftAnchor(8.from(avatarView.leftAnchor).priority(.required))
}) // -> UIView (with already added subviews)
// and you can continue chainable-configuration (for example by specifying own anchors)
.heightAnchor(100) // -> AutoLayoutItem
.activate() // -> UIView
How to extend chaining functionality?
First way – write type extension with function that return self:
There is huge number of layout frameworks… The main goal of creating another one framework was not to create another one framework, but to *make tools for rapid writing of layout code SnapKit, many people know, is one of these solutions, but I decided to make a more declarative and simple solution.\
P.S. in previous versions the Framework was based on DSL SnapKit, but now has its own, more type-safe.
This README decription is all you need to know, no redundant documentation needed.\
💉 You can easily integrate the framework into project and combine it with old/existing layout code.
DeclarativeLayoutKit
Declarative and type-safe framework for fast UI layout
Overview
Requirements
Usage
🚀 Property chaining
Absolutely all mutable properties are represented in the function using code generation powered by Sourcery.
Currently, property chaining is supported in the following types:
UIView
,UIControl
,UILabel
,UIImageView
,UIScrollView
,UITextView
,UITableView
,UICollectionView
,UITextField
,UIButton
,UISlider
,UISwitch
,UIStackView
.😋 And some extra syntactic sugar:
Assignable to variable:
Closure based actions and gestures:
👨👨👦👦 UIStackView extra_most_usefull helpers
Preconfigured stack initializer:
Declarative spacing:
@ArrangedViewBuilder functionBuilder:
🧩 Declarative Constraint Builder
You can set constraints using the same chainable style with anchor constraint.
The return type of builder will be
AutoLayoutItem
– a simple storage of constants instructions. To build and activate them, just callactivate()
function.🧮 Constraint Builder DSL Specification
Attributes
constant
- Int/Float/CGFloat/…target (only relative constraints):
Example:
firstView.rightAnchor(24.from(secondView.leftAnchor))
Example:
subview.leftAnchor(16.to(superview.leftAnchor))
Example:
subview.leftAnchor(16)
equivalent to the previous examplerelationType
Equal
(by default)orLess
Example:
15.orLess
;15.to(secondView.leftAnchor).orLess
orGreater
Example:
15.orGreater
;15.to(secondView.leftAnchor).orGreater
priorioty
15.priority(.defaultLow)
15.priority(250)
multiplier (only dimension constraints)
Example:
headerView.heightAnchor(backgroundView.multiplied(0.5).orLess)
Final Formula:
The order of the attributes is arbitrary:
Extra anchors:
horizontalAnchor
– leftAnchor + rightAnchorExample:
subview.horizontalAnchor(16)
verticalAnchor
– topAnchor + bottomAnchorExample:
subview.verticalAnchor(16.orLess)
centerAnchor
- centerXAnchor + centerYAnchorExample:
avatarView.centerAnchor(backgroundView)
sizeAnchor
- widthAnchor + heightAnchorExample:
avatarView.sizeAnchor(60)
edgesAnchors(insets: UIEdgeInsets, to target: UIView?, priority: UILayoutPriority)
- combination of the left|top|right|bottom anchors.layout(_ builder: (UIView) -> AutoLayoutItem)
- self-related anchorExample:
myView.layout({ $0.heightAnchor($0.widthAnchor) })
🧩 View/Builder Composition
Or using a convenience initializer
ℹ️ NOTE: if you add constraint-builders to a
UIView
(i.e. don’t useanchor-chaining
before calladd(...)
), constraints activations will occur immediately after adding insuperview
. In other words, the return type of theactivate()
function will beSelf
(i.e.UIView
)How to extend chaining functionality?
First way – write type extension with function that return self:
Second way – using Sourcery apply Chainable template with your custom view (see tutorial).
How this framework differs from the others?
There is huge number of layout frameworks…
The main goal of creating another one framework was not to create another one framework, but to *make tools for rapid writing of layout code
SnapKit, many people know, is one of these solutions, but I decided to make a more declarative and simple solution.\
This
README
decription is all you need to know, no redundant documentation needed.\💉 You can easily integrate the framework into project and combine it with old/existing layout code.
Installation
CocoaPods
Replace
YOUR_TARGET_NAME
and then, in thePodfile
directory, type:Swift Package Manager
Create a
Package.swift
file.Credits
License
DeclarativeLayoutKit is released under the MIT license. See LICENSE for details.