SnapKit的扩展------添加数组控制约束,和九宫格布局,等宽,等间距等布局方式

SnapKit的扩展------添加数组控制约束,和九宫格布局,等宽,等间距等布局方式

SnapKit是Swift中的一个很好用的手动控制约束的三方库
这里我就不介绍了
https://github.com/SnapKit/SnapKit
很多从OC转Swift的开发者,如果之前有使用Marsonry的话.其肯定会第一时间替换使用SnapKit

是的SnapKit就是一个类似Marsonry的库,
但是其稍微有点不足,
其没有Marsonry中对Array的扩展,也没有对等宽,等间距等方便的布局方式
所以使用SnapKit,我们有时候需要些很多约束条件来满足某些特殊的需求

这里我做了简单的扩展

https://github.com/spicyShrimp/SnapKitExtend

这里写图片描述

//数组布局

let arr = [view1, view2, view3, view4]

        

arr.snp.makeConstraints{

    $0.width.height.equalTo(100)

    $0.center.equalTo(CGPoint(x: CGFloat(arc4random_uniform(300)) + 50,

                              y: CGFloat(arc4random_uniform(300)) + 50))

}

这里写图片描述

let arr = [view1, view2, view3, view4]

         

arr.snp.makeConstraints{

    $0.width.height.equalTo(100)

}

view1.snp.makeConstraints{ $0.top.equalTo(0) }

view2.snp.makeConstraints{ $0.top.equalTo(100) }

view3.snp.makeConstraints{ $0.top.equalTo(200) }

view4.snp.makeConstraints{ $0.top.equalTo(300) }

这里写图片描述

let arr = [view1, view2, view3, view4]

//        axisType:方向

//        fixedSpacing:中间间距

//        leadSpacing:左边距(上边距)

//        tailSpacing:右边距(下边距)

arr.snp.distributeViewsAlong(axisType: .horizontal, fixedSpacing: 10, leadSpacing: 10, tailSpacing: 10)

//        上面的可以约束x+w,还需要另外约束y+h

//        约束y和height()如果方向是纵向,那么则另外需要设置x+w

arr.snp.makeConstraints{

    $0.top.equalTo(100)

    $0.height.equalTo(CGFloat(arc4random_uniform(100) + 50))

}

这里写图片描述

let arr = [view1, view2, view3, view4]

        

//        axisType:方向

//        fixedItemLength:item对应方向的宽或者高

//        leadSpacing:左边距(上边距)

//        tailSpacing:右边距(下边距)

arr.snp.distributeViewsAlong(axisType: .vertical, fixedItemLength: 100, leadSpacing: 30, tailSpacing: 30)

//        上面的可以约束y+h,还需要另外约束x+w

//        约束y和height()如果方向是纵向,那么则另外需要设置y+h

arr.snp.makeConstraints{

    $0.width.left.equalTo(100)

}

这里写图片描述

var arr: Array<ConstraintView> = [];

for _ in 0..<9 {

    let subview = UIView()

    subview.backgroundColor = UIColor.random

    view.addSubview(subview)

    arr.append(subview)

}

//        固定大小,可变中间间距,上下左右间距默认为0,可以设置

arr.snp.distributeSudokuViews(fixedItemWidth: 100, fixedItemHeight: 100, warpCount: 3)

这里写图片描述

var arr: Array<ConstraintView> = [];

for _ in 0..<9 {

    let subview = UIView()

    subview.backgroundColor = UIColor.random

    view.addSubview(subview)

    arr.append(subview)

}

//        固定间距,可变大小,上下左右间距默认为0,可以设置

arr.snp.distributeSudokuViews(fixedLineSpacing: 10, fixedInteritemSpacing: 10, warpCount: 3)

这份扩展是前段时间写的,一直忘记分享出来,今天时间比较空闲,想起来,就拿出来分享给大家了

具体的demo
前往下载即可

https://github.com/spicyShrimp/SnapKitExtend

发布了15 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sundaysme/article/details/100773772
今日推荐