SnapKit简易教程

SnapKit是Swift中用于简化AutoLayout的库,类似于Objective-C的Masonry。本文介绍了SnapKit的基本用法,如设置视图相对于父容器的约束,保存并更新约束进行动画,链式设计模式,重设约束以及更新已有约束,展示了其在iOS开发中的易用性和灵活性。通过示例代码,开发者可以快速掌握SnapKit在实际项目中的应用。
摘要由CSDN通过智能技术生成

语言: swift, 版本:swift5,XCode:10.2
写作时间:2019-06-19

Introduction to SnapKit: Make Auto Layout Easy for iOS App Development

SnapKit简介

相对布局,Swift用SnapKit,跟Objc用Masonry是一个团队维护的,所以语法糖都一毛一样。github Star > 1.5w, 信得过!
在这里插入图片描述
Demo gif:

1. 相对于父容器的布局,80%场景都可解决

注意:设置相对布局之前,前置条件为把addSubview加入父视图

跟父界面的布局一毛一样,也就是逆时针方向上左下右,可以设置距离边界偏移量offset(上和左是用正数,下和右是用负数表示缩进), 设置优先级priority,默认是1000; 常用情况的subview的是3个方向跟父界面相对,缺少一个方向,用高度或者宽度定位:

		viewTop = UIView()
        viewContainer.addSubview(viewTop)
        
        viewTop.backgroundColor = innerViewBGColor
        viewTop.snp.makeConstraints { (make) in
        	make.top.equalTo(viewContainer).offset(40).priority(750)
            make.left.equalTo(viewContainer)
            make.bottom.equalTo(viewContainer)
            make.right.equalTo(viewContainer)
            //make.height.equalTo(100)
        }
  •  

如果跟父视图都一致,有内边距可以用UIEdgeInsets:

lblTitle.snp.makeConstraints { (make) in
            make.edges.equalTo(viewTop).inset(UIEdgeInsets(top: 0.0, left: 16.0, bottom: 0.0, right: 0.0))
        }
  •  

2. 保存约束条件Constraint,做动画的时候可以直接修改约束

保存约束self.centerYConstraint,实际上上面Y方向已经居中

func setupContainerView() {
        viewContainer = UIView()
        self.addSubview(viewContainer)
        
        viewContainer.snp.makeConstraints { (make) in
            self.centerYConstraint = make.centerY.equalTo(self).constraint
        }
    }
  •  

更新Y方向向下偏移10px:

centerYConstraint.update(offset: 10)

self.setNeedsLayout()

UIView.animate(withDuration: 0.4) {
    self.layoutIfNeeded()
}
  •  

3. 链式设计模式

上面为啥centerYConstraint跟make的用法一样? 这是链式设计模式,也就是left, top, centerY, 等方法返回都是self自己。比如left, top都是跟父视图一样,也可以这么写:

        viewTop.snp.makeConstraints { (make) in
        	make.left.top.equalTo(viewContainer)
        	// the same as below
        	// make.left.equalTo(viewContainer)
        	// make.top.equalTo(viewContainer)
        }
  •  

看源码是如何使用链式设计模式的:

public var left: ConstraintMakerExtendable {
    self.description.attributes += .left
    return self
}

public var top: ConstraintMakerExtendable {
    self.description.attributes += .top
    return self
}

public var bottom: ConstraintMakerExtendable {
    self.description.attributes += .bottom
    return self
}

public var right: ConstraintMakerExtendable {
    self.description.attributes += .right
    return self
}

  •  

4. 4. 重新设置约束remakeConstraints

清空原有约束,重新定义约束用remakeConstraints:

txtEmail.snp.remakeConstraints { (make) in
     make.top.equalTo(viewTop.snp.bottom).offset(16)
     make.left.equalTo(viewContainer).offset(8)
     make.right.equalTo(viewContainer).offset(-8)
     make.height.equalTo(textfieldHeight)
 }

  •  

5. 5. 更新已经存在的约束

如果原有约束存在,需要更新约束 updateConstraints:

activityIndicator.snp.updateConstraints { (make) in
    make.centerY.equalTo(viewContainer).offset(-containerViewHeight/2 - 20)
}
  •  

6. 为啥不会循环引用

view自己调用的方法,方法中的参数make又是自己,为啥不会循环引用

btnConnect.snp.makeConstraints { (make) in
    make.top.equalTo(viewBottom)
    make.right.equalTo(viewBottom)
    make.bottom.equalTo(viewBottom)
    make.width.equalTo(connectButtonWidth)
}
  •  

看源码可以知道端倪,方法中的block没有被self引用,也就不会存在循环引用。

public func makeConstraints(_ closure: (_ make: ConstraintMaker) -> Void) {
        ConstraintMaker.makeConstraints(item: self.view, closure: closure)
    }
  •  

7. 总结

SnapKit 用第一节的方法可以解决80%的常见布局功能,有动画需求参照下面的3个小节也可以解决。完整教程请参照:Introduction to SnapKit: Make Auto Layout Easy for iOS App Development

代码下载

https://github.com/zgpeace/SnapkitDemo

参考

https://www.appcoda.com/snapkit/
https://github.com/SnapKit/SnapKit
https://github.com/SnapKit/Masonry

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值