A simple library(only 200 lines of code of Core) make UIKit declaretive like SwiftUI, no custom component, all UIKit original component.
Use EGChainable to call API chainable.
Use RxSwift to make UI component update automatic when data update.
Also work perfectly with Autolayout or it's API wrapper like SnapKit, no limit when apply layout constraint in resulutBuilder.
When something DeclarativeUIKit can't do, you can do it imperative way, like normal UIKit usage, it's ok.
DeclarativeUIKit/Example/DeclarativeUIKit/ViewController.swift
Lines 22 to 144 in 5a4a20a
class ViewController: UIViewController { | |
@RxUIPublished var text = "Hello" | |
@RxUIPublished var text1 = "Hello" | |
var stack: UIStackView? | |
var ahh: [UIView]? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { | |
self.text1 = "World" | |
} | |
view.body { | |
[UIView().C.backgroundColor(.red), UILabel()].asBuildItems | |
UIView().C.backgroundColor(.red) | |
UIView().C.backgroundColor(.red).asView() | |
UILabel().apply { label in | |
label.R.text(Observable.combineLatest($text, $text1).flatMap { PublishSubject.just([$0, $1].joined(separator: " ")) }) | |
label.C.textColor(.black) | |
.text("Hello, World!") | |
label.S.makeConstraints { make in | |
make.centerX.equalToSuperview() | |
make.top.equalToSuperview().offset(70) | |
} | |
} | |
UIView { | |
UIView().alias("Round cornder backgound").body { | |
UIStackView.V { | |
UIListView(with: NSLayoutConstraint.Axis.horizontal) { | |
for i in 0..<50 { | |
UILabel().apply { label in | |
label.C.text("\(i)").textAlignment(.center) | |
label.C.backgroundColor(.random) | |
label.S.makeConstraints { make in | |
make.width.equalTo(30) | |
} | |
} | |
} | |
}.apply { view in | |
view.S.makeConstraints { maker in | |
maker.height.equalTo(30) | |
maker.leading.trailing.equalToSuperview() | |
} | |
} | |
["ahh", "ahhh"].map { s in | |
UILabel().apply { label in | |
label.C.text(s).textAlignment(.center) | |
} | |
}.assign(to: &ahh) | |
UILabel().alias("Title").apply { label in | |
label.text = "Delete your account?" | |
}.spacingAfterSelf(8) | |
UIStackView.H { | |
UILabel().apply { label in | |
label.text = "ahh" | |
} | |
UILabel().alias("Red Alert").apply { label in | |
label.text = "Deleting account is permanent" | |
label.textColor = .red.withAlphaComponent(0.4) | |
} | |
} | |
UILabel().alias("Long text, desc of alert.").apply { label in | |
label.text = "Your Favourite Meditation and Music、your progress of mediation journey will be permanently deleted." | |
label.numberOfLines = 0 | |
label.textAlignment = .center | |
label.S.makeConstraints { make in | |
make.leading.equalToSuperview().offset(16) | |
make.trailing.equalToSuperview().offset(-16) | |
} | |
}.spacingAfterSelf(20) | |
CircleView { | |
UIButton().apply { button in | |
button.C.setTitle("ahhhhhhhhhhhhhhhh", for: .normal).setTitleColor(.white, for: .normal) | |
button.S.makeConstraints { make in | |
make.edges.equalToSuperview() | |
make.width.equalTo(250) | |
make.height.equalTo(40) | |
} | |
} | |
}.apply { view in | |
view.backgroundColor = .red.withAlphaComponent(0.6) | |
}.spacingAfterSelf(10) | |
CircleView { | |
UIButton().apply { button in | |
button.setTitle("Cancel", for: .normal) | |
button.setTitleColor(.white, for: .normal) | |
button.S.makeConstraints { make in | |
make.edges.equalToSuperview() | |
make.width.equalTo(250) | |
make.height.equalTo(40) | |
} | |
} | |
}.apply { view in | |
view.backgroundColor = .gray.withAlphaComponent(0.6) | |
} | |
UISpace() | |
}.apply { stack in | |
stack.alignment = .center | |
stack.S.makeConstraints { make in | |
make.edges.equalToSuperview() | |
} | |
}.assign(to: &stack) | |
}.apply { view in | |
view.backgroundColor = .white | |
view.layer.cornerRadius = 8 | |
view.clipsToBounds = true | |
view.S.makeConstraints { make in | |
make.width.equalTo(300) | |
make.height.equalTo(400) | |
make.center.equalToSuperview() | |
} | |
} | |
}.apply { view in | |
view.backgroundColor = UIColor.black.withAlphaComponent(0.5) | |
view.S.makeConstraints { make in | |
make.edges.equalToSuperview() | |
} | |
} | |
} | |
} |
To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS 10+
DeclarativeUIKit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'EGDeclarativeUIKit'
EkkoG, beijiu572@gmail.com
DeclarativeUIKit is available under the MIT license. See the LICENSE file for more info.