SnapKit知识点整理
SnapKit是一种DSL,可在iOS和OS X上简化自动布局。
安装:
Cocoapods:
1 2 3 4 5 6
| platform :ios, '10.0' use_frameworks!
target '<你的项目名>' do pod 'SnapKit', '~> 5.0.0' end
|
Carthage:
1 2 3
| $ brew update $ brew install carthage github "SnapKit/SnapKit" ~> 5.0.0
|
使用:
1、实现一个宽高为100,居于当前视图的中心的视图布局,示例代码如下
1 2 3 4 5 6 7
| let testView = UIView() testView.backgroundColor = UIColor.cyan view.addSubview(testView) testView.snp.makeConstraints { (make) in make.width.height.equalTo(100) make.center.equalToSuperview() }
|
2、View2位于View内并且距离View1的边距均为20px
1 2 3 4 5 6 7 8 9 10 11 12 13
| let view1 = UIView() view1.frame = CGRect(x: 0, y: 0, width: 300, height: 300) view1.center = view.center view1.backgroundColor = UIColor.black view.addSubview(view1)
let view2 = UIView() view2.backgroundColor = UIColor.red view1.addSubview(view2) view2.snp.makeConstraints { (make) in make.edges.equalToSuperview().inset(UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))}
|
3、Snapkit 的 greaterThanOrEqualTo 属性