HGCodeBaseUI
这提出了一种不使用Interface Builder编写UI的设计。
使用代码编写UI的设计
protocol UI
UI
协议表示任何类都遵守此协议,以取代Interface Builder的责任。
protocol UIOwner
UIOwner
协议表示任何类都符合此协议,表示可以拥有符合UI
协议的对象的对象。
示例
使用UIViewController - UI
final class SomeViewController: UIViewController, UIOwner {
var viewUI: SomeUI!
override func loadView() {
// Instantiates `viewUI` at `loadView()`.
viewUI = .init(owner: self)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do something with `viewUI.label`...
}
}
final class SomeUI: UI {
// It is recommended to declare it as `unowned`.
unowned var owner: SomeViewController
// It is recommended to declare any UI component variables using `lazy` keyword.
lazy var label = UILabel()
init(owner: SomeViewController) {
self.owner = owner
// **Important**
// You must write the following line because `owner.view` isn't made yet.
owner.view = .init()
// You should write the following line
// because the default value of `UIView.backgroundColor` is `nil`,
// which results in a transparent background color.
view.backgroundColor = .white
// By using `do` method of `Then`, we can configure the label like this...
label.do {
$0.text = "Hello World!"
$0.textColor = .blue
$0.font = .systemFont(ofSize: 15)
$0.numberOfLines = 1
$0.lineBreakMode = .byWordWrapping
}
}
}
使用UIView - UI
final class SomeView: UIView, UIOwner {
var viewUI: SomeUI!
override init(frame: CGRect) {
super.init(frame: frame)
// Instantiates `viewUI` at any initializer.
viewUI = .init(owner: self)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
final class SomeUI: UI {
unowned var owner: SomeView
lazy var label = UILabel()
init(owner: SomeView) {
self.owner = owner
label.do {
$0.text = "Hello World!"
$0.textColor = .blue
$0.font = .systemFont(ofSize: 15)
$0.numberOfLines = 1
$0.lineBreakMode = .byWordWrapping
}
}
}
常用项包裹器
UIButton
戳外部的击中
控制事件
// Same as `addTarget(self, action: #selector(buttonDidTap(_:), for: .touchUpInside)`
button.tapped(onTarget: self, action: #selector(buttonDidTap(_:)))
UIView
手势识别器
// Same as `UITapGestureRecognizer(target: self, action: #selector(viewDidTap(_:))`
view.addGestureRecognizer(.tap, onTarget: self, action: #selector(viewDidTap(_:)))
UISlider
值已改变
控制事件
// Same as `addTarget(self, action: #selector(sliderValueChanged(_:), for: .valueChanged)`
slider.valueChanged(onTarget: self, action: #selector(sliderValueChanged(_:)))
UITextField
编辑已改变
控制事件
// Same as `addtarget(self, action: #selector(textFieldTextDidChange(_:), for: .editingChanged)`
textField.textChanged(onTarget: self, action: #selector(textFieldTextDidChange(_:)))
UI边缘间隔
// Same as `UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 4)`
textView.textContainerInset = .all(4)
// Same as `UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 4)`
someButton.titleEdgeInsets = .symmetric(horizontal: 8, vertical: 4)
// Same as `UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 0)`
tableView.separatorInset = .left(4)
CGRect
/CGSize
/CGPoint
// Same as `CGRect(x: 100, y: 100, width: 100, height: 100)`
label.frame = .all(100)
// Same as `CGSize(width: 100, height: 100)`
label.frame.size = .all(100)
// Same as `CGPoint(width: 100, height: 100)`
label.frame.origin = .all(100)
安装
HGCodeBaseUI 只支持 Cocoapods。
CocoaPods 是一个用于 Cocoa 项目的依赖管理器。您可以使用以下命令进行安装
$ gem install cocoapods
要使用 CocoaPods 将 HGCodeBaseUI 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
target '<Your Target Name>' do
pod 'HGCodeBaseUI'
end
然后,运行以下命令
$ pod install
需求
- Swift 4
- iOS 10
依赖项
许可证
HGCodeBaseUI 受 MIT 许可证约束。有关更多信息,请参阅 LICENSE。