LayoutKit 是 iOS、macOS 和 tvOS 的快速视图布局库。
动机
LinkedIn 创建 LayoutKit 是因为我们发现 Auto Layout 并不具备足够的性能来满足可滚动视图中复杂视图层次结构的布局需求。更多信息,请阅读博客文章。
好处
LayoutKit 相比使用 Auto Layout 有很多优点
- 快:LayoutKit 的速度和手动布局代码一样快,并且比 Auto Layout 要快得多。
- 异步:布局可以在后台线程中计算,因此不会打断用户交互。
- 声明式:布局使用不可变数据结构声明。这使得布局代码更容易开发、文档化、代码审查、测试、调试、分析和维护。
- 可缓存:布局结果是不可变的数据结构,因此可以在后台预先计算并缓存以增加用户的感知性能。
LayoutKit 还提供了一些使它像 Auto Layout 一样容易使用的优点
- 与UIKit兼容:LayoutKit 生成 UIView,并提供了一个适配器,使其很容易与 UITableView 和 UICollectionView 一起使用。
- 国际化:LayoutKit 自动调整视图框架以支持从右到左的语言。
- Swift:LayoutKit 可用于 Swift 应用程序和 Playgrounds。
- 已测试和准备用于生产:LayoutKit 被单元测试所覆盖,并且正在 LinkedIn 和 LinkedIn 求职 iOS 应用程序的最新版本中使用。
- 开源:它不是像Auto Layout那样的黑盒。
- Apache License (v2):您的律师会高兴地知道这里没有任何专利伎俩。
你好,世界
let image = SizeLayout<UIImageView>(width: 50, height: 50, config: { imageView in
imageView.image = UIImage(named: "earth.jpg")
})
let label = LabelLayout(text: "Hello World!", alignment: .center)
let stack = StackLayout(
axis: .horizontal,
spacing: 4,
sublayouts: [image, label])
let insets = UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 8)
let helloWorld = InsetLayout(insets: insets, sublayout: stack)
helloWorld.arrangement().makeViews(in: rootView)
限制
我们发现LayoutKit是一个有用的工具,但您应该知道它不是什么。
- LayoutKit不是一个基于约束的布局系统。如果您想要表达视图间的约束,那么这些视图需要是单个布局的子视图,并且该布局需要实现代码以强制执行该约束。
- LayoutKit不是flexbox,但您可能发现两者有相似之处。
安装
LayoutKit可以使用CocoaPods或Carthage进行安装。
CocoaPods
将以下内容添加到您的Podspec中
pod 'LayoutKit'
然后运行pod install
。
Carthage
将以下内容添加到您的Cartfile中
github "linkedin/LayoutKit"
然后运行carthage update
。
文档
现在你可以开始构建用户界面。