ChLayout is small framework for declarative UI building.
Carthage:
github "sionyx/ChLayout"
- Create views with
ch.create()
function. - Set properties with extended functions and chain them into rows.
- Combine views into hierarchical layouts.
override func viewDidLoad() {
super.viewDidLoad()
view.safeAreaLayout {
UIView.ch.create()
}
}
override func viewDidLoad() {
super.viewDidLoad()
view.verticalStackLayout {[
UILabel.ch.create(),
UIImageView.ch.create(),
UIButton.ch.create(),
]}
}
override func viewDidLoad() {
super.viewDidLoad()
view.safeAreaLayout {
UIScrollView.ch.create()
.scrollableVerticalStackLayout {[
UILabel.ch.create(),
UIImageView.ch.create(),
UIButton.ch.create(),
]}
}
}
var titleLabel: UILabel!
var coverImageView: UIImageView!
var submitButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
view.verticalStackLayout {[
UILabel.ch.create()
.ch.named(&titleLabel),
UIImageView.ch.create(),
.ch.named(&coverImageView),
UIButton.ch.create(),
.ch.named(&submitButton),
]}
}
var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
view.safeAreaLayout {
UILabel.ch.create()
.ch.named(&titleLabel)
.text("ChLayout")
.textColor(.blue)
}
}
var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
view.safeAreaLayout {
UILabel.ch.create()
.ch.named(&titleLabel)
.text("ChLayout")
.textColor(.blue)
.ch.setup {
$0.layer.borderWidth = 1
$0.layer.cornerRadius = 8
}
}
}
var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
view.safeAreaLayout {
UILabel.ch.create()
.ch.named(&titleLabel)
.ch.set(\.text, "ChLayout")
.ch.set(\.backgroundColor, .blue)
.ch.set(\.layer.borderWidth, 1)
}
}
Note: Avoid using .ch.set() for properties that already have methods in ChLayout
var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
titleLabel = UILabel(...)
view.safeAreaLayout {
titleLabel
.text("ChLayout")
.textColor(.blue)
}
}
var fancyView: MyFancyView!
override func viewDidLoad() {
super.viewDidLoad()
view.safeAreaLayout {
MyFancyView.ch.fromXib()
.ch.named(&fancyView)
.ch.setup {
$0.propertyOne = 1
$0.propertyTwo = "2"
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
view.safeAreaLayout {
UILabel.ch.create()
.ch.exec(\.myConfigureFont)
}
}
extension UILabel {
var myConfigureFont: (() -> Void) {
return _myConfigureFont
}
private func _myConfigureFont() {
font = UIFont.systemFont(ofSize: 24)
}
}
Note: Same method perform() can be used for NSObject based classes:
// Call to sizeToFit is shown only as demonstration of perform syntax!
UILabel.ch.create()
.ch.perform(#selector(UIView.sizeToFit))
- UIView.fillLayout - child view(s) will be located inside parent view
- UIView.safeAreaLayout - child views(s) will be located inside safe area
- UIView.verticalSplitLayout - two views will be located vertically
- UIView.horizontalSplitLayout - two views will be located next to each other
- UIView.verticalStackLayout - child views will be located one under another
- UIView.positionLayout - child view will be positioned to the left/center/right and to the top/center/bottom
- UIView.floodLayout - child views will be alligned to one our two columns
- UIScrollView.scrollableVerticalStackLayout - child view will be located into scrollable stack
- .margins(_:) - sets specified margins to view
- .width(_:) - sets width constraint to view
- .height(_:) - sets height constraing to view
- UIView
- margins
- zeroMargins - shorten for margins(.zero)
- backgroundColor
- whiteBG - shorten for backgroundColor(.white)
- cornerRadius
- borderWidth
- borderColor (accepts UIColor, finally!)
- shadow
- clipToBounds
- hidden
- alpha
- contentHuggingPriority
- horizontalContentHuggingPriority
- verticalContentHuggingPriority
- UILabel
- text
- font
- textColor
- textAlignment
- numberOfLines
- adjustsFontSizeToFitWidth
- minimumScaleFactor
- UIImageView
- image
- imageNamed - shorten for image(UIImage(named: ))
- contentMode
- UIButton
- title
- titleColor
- titleFont
- image
- imageNamed
- backgroundImage
- backgroundImageNamed
- contentEdgeInsets
- titleEdgeInsets
- imageEdgeInsets
- UITextField
- text
- font
- textColor
- textAlignment
- placeholder
- placeholder(: color:)
- attributedPlaceholder
- placeholder(: color:) - specific version of attributedPlaceholder
- UIScrollView
- bounceHorizontal
- bounceVertical
- horizontalScrollIndicator
- verticalScrollIndicator
- contentInset
- contentInsetAdjustmentBehavior
- UICollectionView
- allowsSelection
- UITableView
- allowsSelection
- separatorStyle
- separatorColor
- UIActivityIndicatorView
- color
- style
- hidesWhenStopped
- UIRefreshControl
- tintColor
- attributedTitle
- .addSubview - inline method to add subview
- .insertSubview - inline method to insert subview
- .ch.setup - inline access to object
- .ch.set - inline method to set property by keypath
- .ch.exec - inline method to call block property by keypath
- .ch.perform - inline method to perform selector for classes based on NSObject