Layoutable
Layoutable 是苹果 AutoLayout 的 swift 实现。它以其核心的 same Cassowary 算法为基础,并提供了一组类似于 AutoLayout 的 API。不同之处在于 Layoutable 更灵活,更易于使用。Layoutable 不依赖于 UIView,它可以在任何符合 Layoutable 协议的对象中使用,例如 CALaye 或自定义对象。它可以在后台线程中使用,这是 Layoutable 的核心优势。Layoutable 还提供了高级 API 和语法糖,以便易于使用。
要求
- iOS 8.0+
- Swift 4.2
- Xcode 10.0 或更高版本
安装
Layoutable 依赖于 Cassowary,您需要在项目中添加这两个。
Carthage
Carthage 是 Cocoa 应用的分布式依赖管理器。要安装 Carthage 工具,您可以使用 Homebrew。
$ brew update
$ brew install carthage
将Panda集成到您的Xcode项目中,并使用Carthage,请在您的Cartfile
中指定它。
github "https://github.com/nangege/Layoutable" "master"
然后,运行以下命令来构建Panda框架:
$ carthage update
最后,您需要手动设置您的Xcode项目,以添加Panda、Layoutable和Cassowary frameworks。
在您的应用程序目标“常规”设置标签页中,“链接框架和库”部分,将Carthage/Build文件夹中的每个框架拖放到菜单中。
在您的应用程序目标“构建目标”设置标签页中,单击“+”图标并选择“新运行脚本phase”。创建一个运行脚本,内容如下:
/usr/local/bin/carthage copy-frameworks
并在“输入文件”下添加您要使用的框架路径。
$(SRCROOT)/Carthage/Build/iOS/Layoutable.framework
$(SRCROOT)/Carthage/Build/iOS/Cassowary.framework
有关使用Carthage的更多信息,请参阅其项目页面。
手动
`git clone [email protected]:nangege/Layoutable.git` ,
`git clone [email protected]:nangege/Cassowary.git`
然后将Layoutable.xcodeproj
和Cassowary.xcodeproj
文件拖到您的项目
在您的应用程序目标“常规”设置标签页中,“链接框架和库”部分,添加Layoutable.framework
和Cassowary.framework
。
使用
-
定义您自己的布局对象
import Layoutable class TestNode: Layoutable{ public init() {} lazy var manager = LayoutManager(self) var layoutSize = InvaidIntrinsicSize weak var superItem: Layoutable? = nil var subItems = [Layoutable]() func addSubnode(_ node: TestNode){ subItems.append(node) node.superItem = self } func layoutSubItems() {} func updateConstraint() {} var layoutRect: Rect = RectZero var itemIntrinsicContentSize: Size{ return layoutSize } func contentSizeFor(maxWidth: Double) -> Size { return InvaidIntrinsicSize } }
-
使用布局对象进行布局
import Layoutable // Layout node1 and node2 horizontalally in node,space 10 and equal center in Vertical let node = TestNode() let node1 = TestNode() let node2 = TestNode() node.addSubnode(node1) node.addSubnode(node2) node1.size == (30,30) node2.size == (40,40) [node,node1].equal(.centerY,.left) [node2,node].equal(.top,.bottom,.centerY,.right) [node1,node2].space(10, axis: .horizontal) node.layoutIfEnabled() print(node.frame) // (0.0, 0.0, 80.0, 40.0) print(node1.frame) // (0.0, 5.0, 30.0, 30.0) print(node2.frame) // (40.0, 0.0, 40.0, 40.0)
操作
-
基本属性
与Autolayout类似,Layoutable支持Equal、lessThanOrEqual和greatThanOrEqualTo。
node1.left.equalTo(node2.left) node1.top.greatThanOrEqualTo(node2.top) node1.bottom.lessThanOrEqualTo(node2.bottom)
或
node1.left == node2.left // can bve write as node1.left == node2 node1.top >= node2.top // can bve write as node1.top >= node2 node1.bottom <= node2.bottom // can bve write as node1.bottom <= node2
-
组合属性
除了基本的left、right属性之外,Layoutable还提供了一些组合属性,如size、xSide、ySide、edge等。
node1.xSide.equalTo(node2,insets:(10,10)) node1.edge(node2,insets:(5,5,5,5)) node.topLeft.equalTo(node2, offset: (10,5))
或
node1.xSide == node2.xSide + (10,10) //node1.xSide == node2.xSide.insets(10) //node1.xSide == node2.xSide.insets((10,10)) node1.edge == node2.insets((5,5,5,5)) // node1.edge == node2 + (5,5,5,5) node.topLeft == node2.topLeft.offset((10,5))
-
更新优先级
node1.width == 100 ~.strong node1.height == 200 ~ 760.0
-
更新常量
let c = node.left == node2.left + 10 c.constant = 100
支持的属性
Layoutable | NSLayoutAttribute |
---|---|
Layoutable.left | NSLayoutAttributeLeft |
Layoutable.right | NSLayoutAttributeRight |
Layoutable.top | NSLayoutAttributeTop |
Layoutable.bottom | NSLayoutAttributeBottom |
Layoutable.width | NSLayoutAttributeWidth |
Layoutable.height | NSLayoutAttributeHeight |
Layoutable.centerX | NSLayoutAttributeCenterX |
Layoutable.centerY | NSLayoutAttributeCenterY |
Layoutable.size | width和height |
可布局的中心 | 中心X和中心Y |
Layoutable.xSide | 左边和右边 |
Layoutable.ySide | 上边和下边 |
Layoutable.edge | 上,左,下,右 |
Layoutable.topLeft | 上和左 |
Layoutable.topRight | 上和右 |
Layoutable.bottomLeft | 下和左 |
Layoutable.bottomRight | 下和右 |
授权
MIT许可(MIT)