ios开发布局

SnapKit-基础篇

2018-06-03  本文已影响71人  胖子程
SnapKit的介绍

  SnapKit是Swift中的一个第三方自适应布局库,它对应于OC中的Masonry

SnapKit的基本语法与使用

.equalTo/equalToSuperView:等于
.lessThanOrEqualTo:小于等于
.greaterThanOrEqualTo:大于等于
.inset/.offset:位移修正
.multipliedBy:倍率修正

view.snp.left
view.snp.right
view.snp.top
view.snp.bottom
view.snp.leading
view.snp.trailing
view.snp.width
view.snp.height
view.snp.centerX
view.snp.centerY
view.snp.baseline

make.center.equalToSuperview()
OR:
make.centerX.equalToSuperview()

   --2 top,left,bottom,right,width,height

make.top.equalTo()
make.left.equalTo(100)
make.bottom.equalTo()
make.right.equalTo()
make.width.equalTo(100)
make.height.equalTo(100)
OR:
make.top.equalToSuperview()
make.left.equalToSuperview()
make.bottom.equalToSuperview()
make.right.equalToSuperview()
make.width.equalToSuperview()
make.height.equalToSuperview()

  3 内部修正:inset

make.edges.equalTo(someView).inset(UIEdgeInsetsMake(10, 15, 20, 25))

  4 外部修正:offset

make.size.equalTo(someView).offset(-20)

  5 倍率:multiplied

make.size.equalTo(someView).multipliedBy(0.38)
    //保存约束的引用
    var deleteSomeConstraint: Constraint?
....
    deleteSomeConstraint = make.width.height.equalTo(100).constraint
...
    //删除不需要的约束
    deleteSomeConstraint?.deactivate()

   2 更新约束

    var updateSomeConstraint: Constraint?
...
    updateSomeConstraint = make.size.equalTo(someView).offset(-20).constraint
...
    //更新相应的约束:方式一
    updateSomeConstraint?.update(offset: -30)

    //更新相应的约束:方式二
    someView.snp.updateConstraints { (make) in
        make.width.equalTo(200)
    }

   3 重做约束

    someView.snp.remakeConstraints { (make) in
        make.edges.equalTo(anotherView).inset(UIEdgeInsetsMake(25, 30, 5, 5))
    }

   4 最小、最大约束

    //设置当前View不大于self.view的宽度
    make.width.height.lessThanOrEqualTo(self.view.snp.width)
    //设置当前View不小于self.view的宽度
    make.width.height.greaterThanOrEqualTo(self.view.snp.width)

参考:
航歌的技术博客

上一篇下一篇

猜你喜欢

热点阅读