FlexBoxUIKit
基于facebook/yoga实现一个类似swiftui和Flutter的声明式UI框架
Requirements
- iOS 9.0+
- Xcode 12.5
- Swift 5.4
Installation
Cocoapods
pod 'FlexBoxUIKit', :git => 'https://github.com/BestYun/FlexBoxUIKit.git', :tag => '0.0.1'
Usage 用法
Quick Start 快速开始
import FlexBoxUIKit //1.导入FlexBoxUIKit
import UIKit
//2.继承FlexBaseViewController
class ViewController: FlexBaseViewController
{
override func viewDidLoad() {
super.viewDidLoad()
}
//3.重写bodyView
override func bodyView() -> UIView {
return HStackView(mainAxis: .center, crossAxis: .center) {
Text("Hello FlexBoxUIKit")
}
.flex(1)
}
}
or
import FlexBoxUIKit
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
view.flex.mainAxis(.center).crossAxis(.center).addItems {
HStackView(mainAxis: .center, crossAxis: .center) {
Text("Hello FlexBoxUIKit")
}
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
view.flex.applyLayout()
}
}
example1
HStackView {
ZStackView {
ImageView()
.backgroundColor(UIColor.gray.withAlphaComponent(0.5))
.cornerRadius(8)
.left(0)
.bottom(0)
.size(width: 50, height: 50)
Text("1")
.fontSize(12)
.textColor(.white)
.right(0)
.top(0)
.size(16)
.cornerRadius(8)
.backgroundColor(.red)
.textAlignment(.center)
}
.size(58)
.margin(.right, 8)
VStackView(mainAxis: .spaceAround) {
HStackView(crossAxis: .center) {
Text("Leo")
.fontSize(16, weight: .bold)
.expanded()
Text("13:30")
.fontSize(12, weight: .medium)
.textColor(.gray)
}
Text("hello,nice to meet you")
}
.height(50)
.expanded()
.margin(.top, 8)
}
.padding(.horizontal, 15)
.margin(.top, 100)
Documentation
https://reactnative.cn/docs/flexbox
Modifier chain 链式语法
UILabel()
.modifier
.text("链式语法")
.textColor(.orange)
.font(.systemFont(ofSize: 16))
.view
等同于
let label = UILabel()
label.text = "test apply"
label.font = .systemFont(ofSize: 16)
label.textColor = .orange
apply sugar 只在UIView有效
UIView(frame: CGRect(x: 10, y: 100, width: 60, height: 60)).apply {
$0.backgroundColor = .blue
$0.layer.cornerRadius = 30
$0.clipsToBounds = true
}
UILabel().apply { label in
label.text = "test apply"
label.font = .systemFont(ofSize: 16)
label.textColor = .orange
}
等同于
let blueView = UIView(frame: CGRect(x: 10, y: 100, width: 60, height: 60))
blueView.backgroundColor = .blue
blueView.layer.cornerRadius = 30
blueView.clipsToBounds = true
let label = UILabel()
label.text = "test apply"
label.font = .systemFont(ofSize: 16)
label.textColor = .orange
Flex makeLayout
UILabel().flex.makeLayout {
$0.margin(.left, 10).margin(.top, 100)
}.apply {
_ = $0.modifier
.text("flex.makeLayout写法")
.font(.systemFont(ofSize: 18))
.textColor(.orange)
}
资料
TableView https://github.com/josercc/SwiftTableViewGroup
Todo
-
百分比
-
ImageView 网络加载图片
-
flexbox文档
- justifyContent
- alignContent
- alignItems
- alignSelf
- flexDirection
- direction
- flexWrap
- position
-
文档
- UI
-
HStackView = Row
-
VStackView = Column
-
ZStackView = Stack 与Flutter和SwiftUI有差异,需要自己定义好size才有效果
-
Wrap
-
Text
-
ImageView
-
Space
-
TextField
-
TextView
-
ScrollView
-
TabCell
-
TableDynamicCell
-
CollectionCell
-
margin padding left right top bottom
-
size width height minWidth
-
flex 属性
-
applyLayout
-
markDirty
-
sizeThatFits
-
numberOfChildren
-
isIncludedInLayout
-
enabled
-
display
-
状态State
-
demo
- 请求接口后更新数据
可参考的
- https://github.com/MihaelIsaev/UIKitPlus
- https://blog.eppz.eu/declarative-uikit-with-10-lines-of-code/
- https://github.com/hmlongco/RxSwiftWidgets 可以参考它的State实现
- https://tech.youzan.com/-sheng-ming-shi-uikitzai-you-zan-mei-ye-de-shi-jian/
- https://github.com/sakiyamaK/DeclarativeUIKit
- https://github.com/hmlongco/Builder/tree/main
- https://github.com/hainayanda/Draftsman
- https://github.com/nicklockwood/layout xml实现布局
- https://kazaimazai.com/swifty-uikit/
- https://github.com/KazaiMazai/SwiftyUIKit
- https://github.com/zhenglibao/FlexLib
License
FlexBoxUIKit is under MIT license. See the LICENSE file for more info.