SmartUI
描述
SmartUI 是一个受到 SwiftUI 启发的框架。它尽可能地使用与原始内容相同的语法和 UI 开发原则。
背后的主要思想是提供一种简单的代码复制和重用方法,以从 SmartUI 轻松过渡到 SwiftUI,并给开发者一个熟悉声明性 UI 方法的机会。
请访问 文档页面 以找到最佳实践和建议。这将非常有帮助。
所有可用视图和修饰符的完整列表可在 此处 找到。
用法
为了实现类似 SwiftUI 的行为,建议使用 ContainerView 进行布局。 ContainerView 只是一个简单的 UIView,可以在 UIKit 视图层次结构的任何地方使用。
也可以用 SmartUI 视图替换 UIKit 视图的仅一部分,因为 SmartUI 视图会在布局周期中转换为 UIKit 视图并使用 NSLayoutConstraints。
更多详细信息,请参阅 文档。
import UIKit
import SmartUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
ContainerView { [unowned self] in
ScrollView {
VStack {[
Text("?")
.font(Font.system(size: 60, weight: .bold))
.foregroundColor(.white)
.frame(width: 100, height: 100)
.background(Color.blue.opacity(0.4))
.clipShape(Circle())
.padding([.top, .bottom], 40),
Text("Sign In")
.font(Font.system(size: 24, weight: .semibold))
.foregroundColor(accentColor)
.padding(.bottom, 20),
TextField("Username", text: self.username)
.foregroundColor(.black)
.padding(16)
.background(grayBackground)
.cornerRadius(6.0)
.padding(.bottom, 20),
SecureField("Password", text: self.password)
.foregroundColor(.black)
.padding(16)
.background(grayBackground)
.cornerRadius(6.0)
.padding(.bottom, 20)
,
Button("Continue", action: { [unowned self] in
self.view.endEditing(true)
self.handleSignIn()
})
.font(Font.system(size: 20))
.disabled(self.notValid),
]}.padding(16)
}
.bindToKeyboard(extraOffset: 16.0)
}.layout(in: view)
}
}
示例
要运行示例项目,请克隆存储库,然后在 Example 目录中执行 pod install
。
要求
iOS 10 及以上版本。
安装
SmartUI 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile。
pod 'SmartUI'
限制
SmartUI 基于 UIKit 和 NSLayoutConstraints。它试图复制 SwiftUI 的行为,但并非总是可能。最大的问题是使用 UIStackViews 中的多行 UILabel。这就是为什么提供了像 .contentHuggingPriority
和 .contentCompressionResistance
这样的辅助视图修改器,并且 SmartUI Stacks 也支持 SwiftUI Stacks 没有的 .fill
对齐模式。
SmartUI 为不同的布局组件提供了更多的控制,但也需要理解高级 UIKit 原则。熟悉提供的项目示例,这可能会很有帮助。
贡献
该项目被视为 开源。请不要犹豫,积极贡献力量。
作者
伊戈尔·蒂尤卡金,[email protected]
许可证
SmartUI在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。