Suugar
Suugar(发音为 sugar)是一个用 Swift 代码构建 iOS 应用 UI 的实验性项目。
概念
可读的层次结构
Suugar 允许您通过嵌套方法调用编写 UI 层次。
ui {
$0.stack {
$0.image {
$0.image = UIImage(named: "ic_hello")
}
$0.label {
$0.text = "Hello, Suugar!!"
}
}
}
常规 UIKit 组件
所有嵌套都只是常规的方法调用,每个 $0
都是标准的 UIKit 组件。因此,您可以像平常一样用 UIKit 编写代码。
ui {
// $0 is UIView
$0.backgroundColor = UIColor.white
$0.table {
// $0 is UITableView
$0.estimatedRowHeight = UITableView.automaticDimension
$0.register(HelloTableViewCell.self, forCellReuseIdentifier: "cell")
$0.dataSource = self
}
}
易于复用的视图
如果您已经有了自定义视图,您可以轻松将其集成到层次结构中。
class HelloView: UIStackView {
// TODO: Override init methods to call render
private func render() {
ui {
$0.image {
$0.image = UIImage(named: "ic_hello")
}
$0.label {
$0.text = "Hello, Again!!"
}
}
}
}
ui {
// By specifying the type
$0.composite(HelloView.self)
// Or ask compiler to do it
let view: HelloView = $0.composite()
}