SnapKitten
SnapKitten 是一个基于 SnapKit 和 Auto Layout 的线性布局解决方案库。
例子
要运行示例项目,请克隆仓库,然后首先在 Example 目录中运行 pod install
。
用法
import UIKit
import SnapKitten
import PlaygroundSupport
let virtualView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
virtualView.backgroundColor = UIColor.gray
PlaygroundPage.current.liveView = virtualView
// ... some init UI Code
let simpleComponent = Kitten.horizontal().from()
.add(iv).size(40)
.add(lblA).itemOffset(10)
.build()
simpleComponent.backgroundColor = UIColor.green
let threeComponentExample = Kitten.horizontal()
.from().isAlignDirectionEnd(true).defaultAlignment(.center)
.add(iv2).size(40)
.add(lbl2).fillParent()
.add(iv3).size(60)
.build()
threeComponentExample.backgroundColor = UIColor.orange
Kitten.create(.vertical).from(virtualView)
.add(simpleComponent).align(.start)
.add(threeComponentExample)
.build()
那么 Kitten (SnapKitten) 库是什么呢?
Kitten 是 SnapKitten 和 iOS 约束系统下的约束关系构建库。Kitten 在视图之间建立约束关系,从而实现类似 Android LinearLayout 的行为。
小猫简化了复杂的约束系统设置,并将其分解为少数简单的线性关系。
小猫对象创建
创建小猫对象时,需要知道最初的朝向。
//Kitten's method always return Protocol, but you can always force cast to Kitten itself
Kitten.horizontal()
Kitten.vertical()
定义父视图
您可能希望向现有视图添加视图,或者创建一个新的视图。小猫支持滚动视图、视图、UIViewController,并可以动态创建新的视图。对于UIViewController,它默认对齐topLayoutGuide和bottomLayoutGuide。
//all return KittenParentMethods protocol
Kitten.vertical().from()
Kitten.vertical().from(scrollView)
Kitten.vertical().from(viewController)
Kitten.vertical().from(view)
设置父视图
在from()方法之后,您现在可以设置基本设置。KittenParentMethods协议为您提供了一些方法来设置基本布局。
//.center, .start, .end, .parent
defaultAlignment(enum)//a perpendicular alignment of child views
startPadding(int)//offset with first item and parent
endPadding(int)//offset with last item and parent
itemDefaultOffset(int) //offset between item
itemDefaultSideStartPadding(int)//perpendicular offset, always top or left
itemDefaultSideEndPadding(int) //perpendicualr offset, always bottom or right
itemDefaultSidePadding(int)
allPadding(int)//all four direction padding, except item-to-item
isAlignDirectionEnd(bool) //determine if last child align parent's end. Like match_parent in LinearLayout
weightMode(bool) // change to weightMode, child's size based on the weight related to parent, usually use with isAlignDirectionEnd(true)
添加您的第一个子视图
您始终可以将视图添加到Kitten,或者使用KittenChild协议找到现有的视图。
add(UIView)//add a child to Kitten, following KittenChildMethods is updating this child view
with(UIView)//find existing child, and update it in following method call
addChilds(UIView ...)
addChilds([UIView])
设置您的子视图
添加子视图后,您可能需要提供一些有关此视图的信息。
itemOffset(int)//a offset to previous item, for first child, it will not be active
sideStartPadding(int)//a perpendicular padding of item, left or top
sideEndPadding(int)//right or bottom
sidePadding(int)
width(int)//set the width of view
height(int)//set the height of view
size(int)//set the size of view
alignSideStart()//align the parent start, top / left
alignSideEnd()//align parent end, bottom / right
alignSideCenter()//align center of parent, depend on orientation
alignSideParent()//child align parent's width / height
fillParent()//a child try to fit the size to parent as possible
importanceHigh()//the importance of child, higher importance, it will not be compress if other view is big to fillup the parent orientation
importanceMedium()
importanceLow()
weight(float)//active in weightMode(true) only, it determine the weight of item, default is 1.
设置完成后构建约束
必须在将所有子视图添加到Kitten类的build()和rebuild()方法之后构建约束,它们返回包含子视图的父UIView。
build() //will not remove subview / its constraint relation
rebuild() // remove subviews before build()
高级用法
将Kitten对象作为变量
Kitten方法总是会返回"协议",但您始终可以强制转换为Kitten对象
let kitten : Kitten = Kitten.horizonta() as! Kitten
let kitten : Kitten = Kitten.horizonta().from() as! Kitten
let kitten : Kitten = Kitten.horizonta().from().add(lblA) as! Kitten
条件插入子元素
因为Kitten是线性关系,子元素并不与Kitten提供的特定项存在实际关系。Kitten提供了一个条件方法,以允许控制子元素的插入
let threeComponentExample = Kitten.horizontal()
.from().isAlignDirectionEnd(true).defaultAlignment(.center)
.add(iv2).size(40)
.add(lbl2).fillParent()
.add(iv3).size(60).condition({ () -> Bool in
// you can determine the condition by any variable in your class
return false
})
.rebuild()
在iOS Playground测试您的代码
您 always 可以在 iOS Playground 中可视化测试您的代码。要打开可视化 playground,在 Xcode 中点击 "视图 > 辅助编辑器 > 显示辅助编辑器"。
import UIKit
import SnapKitten
import PlaygroundSupport
let virtualView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 100))
virtualView.backgroundColor = UIColor.gray
PlaygroundPage.current.liveView = virtualView
let iv = UIImageView()
iv.backgroundColor = UIColor.red
let lblA = UILabel()
lblA.text = "Hello World"
lblA.backgroundColor = UIColor.blue
let simpleComponent = Kitten.horizontal().from()
.add(iv).size(40)
.add(lblA).itemOffset(10)
.build()
simpleComponent.backgroundColor = UIColor.green
Kitten.create(.vertical).from(virtualView)
.add(simpleComponent).align(.start)
.build()
要求
- iOS 8.0+
安装
SnapKitten可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中
pod "SnapKitten"
作者
Spring, [email protected]
许可证
SnapKitten 可在 MIT 许可证下获得。更多信息请参阅 LICENSE 文件。