第三方布局框架Neon初探

4 篇文章 0 订阅
1 篇文章 0 订阅

github地址:https://github.com/mamaral/Neon

居中

设置 view 在 superview 的中心,调用 anchorInCenter()并设置view大小,相当于frame center

 view1.anchorInCenter(width: 300, height: 300)
 imgV.anchorInCenter(width: 150, height: 150)

屏幕快照 2019-03-18 下午5.50.10.png

填充

如果想 view 填充superview,设置距离superview的边框距离, 调用 fillSuperview()

  imgV.fillSuperview()
  imgV.fillSuperview(left: 20, right: 5, top: 30, bottom: 5)

屏幕快照 2019-03-18 下午5.56.50.png

角对齐

view 相对于 superview 固定于某个角,调用anchorInCorner(),并传入要对齐哪个角的参数

  view1.anchorInCorner(.topLeft, xPad: 20, yPad: 100, width: 100, height: 100)
  view2.anchorInCorner(.topRight, xPad: 20, yPad: 50, width: 100, height: 100)
  view3.anchorInCorner(.bottomLeft, xPad: 20, yPad: 20, width: 100, height: 100)
  view4.anchorInCorner(.bottomRight, xPad: 20, yPad: 30, width: 100, height: 100)

Simulator Screen Shot - iPhone XS Max - 2019-03-18 at 18.01.17.png

边对齐

如果想让 view 相对于 superview 的某一边依靠对齐,调用anchorToEdge,自动居中对齐

   view1.anchorToEdge(.top, padding: 300, width: 100, height: 100)
   view2.anchorToEdge(.left, padding: 0, width: 100, height: 100)
   view3.anchorToEdge(.bottom, padding: 88, width: 100, height: 100)
   view4.anchorToEdge(.right, padding: 0, width: 100, height: 100)

Simulator Screen Shot - iPhone XS Max - 2019-03-18 at 18.04.10.png

边填充

如果想让 view 相对于 superview 的某一边居中对齐并自动填拉伸充,调用anchorAndFillEdge()

 view1.anchorAndFillEdge(.top, xPad: size + 5, yPad: 88, otherSize: size)
 view2.anchorAndFillEdge(.bottom, xPad: size + 5, yPad: 83, otherSize: size)
 view3.anchorAndFillEdge(.left, xPad: 0, yPad: 0, otherSize: size)
 view4.anchorAndFillEdge(.right, xPad: 0, yPad: 0, otherSize: size)

Simulator Screen Shot - iPhone XS Max - 2019-03-18 at 18.08.44.png

相对对齐

距离相对view右边 居 上-中-下
  view1.anchorInCenter(width: 200, height: 200)
  view2.align(.toTheRightMatchingTop, relativeTo: view1, padding: 5, width: 50, height: 50)
  view3.align(.toTheRightMatchingBottom, relativeTo: view1, padding: 5, width: 50, height: 50)
  view4.align(.toTheRightCentered, relativeTo: view1, padding: 5, width: 50, height: 50)

Simulator Screen Shot - iPhone XS Max - 2019-03-18 at 18.14.20.png

距离相对view左边 居 上 中 下
   view2.align(.toTheLeftMatchingTop, relativeTo: view1, padding: 5, width: 50, height: 50)
   view3.align(.toTheLeftMatchingBottom, relativeTo: view1, padding: 5, width: 50, height: 50)
   view4.align(.toTheLeftCentered, relativeTo: view1, padding: 5, width: 50, height: 50)

Simulator Screen Shot - iPhone XS Max - 2019-03-18 at 18.15.48.png

距离相对view下边 居 上 中 下
 view2.align(.underMatchingLeft, relativeTo: view1, padding: 5, width: 50, height: 50)
 view3.align(.underMatchingRight, relativeTo: view1, padding: 5, width: 50, height: 50)
 view4.align(.underCentered, relativeTo: view1, padding: 5, width: 50, height: 50)
距离相对view上边 居 上 中 下
   view2.align(.aboveMatchingLeft, relativeTo: view1, padding: 5, width: 50, height: 50)
   view3.align(.aboveMatchingRight, relativeTo: view1, padding: 5, width: 50, height: 50)
   view4.align(.aboveCentered, relativeTo: view1, padding: 5, width: 50, height: 50)

对齐并填充

不知道或者没法指定一个相对视图的大小,但又要兼顾填充和对齐,并且还依赖与相邻的 view。调用alignAndFill()、alignAndFillWidth()、alignAndFillHeight()

  view1.anchorInCenter(width: 200, height: 200)
  view2.alignAndFillWidth(align: .toTheRightMatchingTop, relativeTo: view1, padding: 5, height: 30)
  view3.alignAndFillHeight(align: .aboveCentered, relativeTo: view1, padding: 50, width: 60)
  view4.alignAndFill(align: .toTheLeftMatchingTop, relativeTo: view1, padding: 5)

Simulator Screen Shot - iPhone XS Max - 2019-03-18 at 18.27.19.png

挤压对齐

如果两个 view 夹着中间一个 view 的需求,调用alignBetweenHorizontal()和alignBetweenVertical()

view1.anchorInCenter(width: 150, height: 300)
view2.align(.toTheRightMatchingTop, relativeTo: view1, padding: 5, width: 100, height: 100)
view3.align(.toTheRightMatchingBottom, relativeTo: view1, padding: 5, width: 100, height: 100)
view4.align(.toTheRightCentered, relativeTo: view1, padding: 5, width: 100, height: 100)
view4.alignBetweenVertical(align: .aboveMatchingRight, primaryView: view3, secondaryView: view2, padding: 10, width: 50)

Simulator Screen Shot - iPhone XS Max - 2019-03-18 at 18.40.08.png

组合

多个view一起组合布局,调用groupInCenter()、groupInCorner()、groupAgainstEdge()

居中对齐组合
 view.groupInCenter(group: .vertical, views: [view1,view2,view3], padding: 10, width: size, height: size)
 view.groupInCenter(group: .horizontal, views: [view1,view2,view3], padding: 20, width: size, height: size)

屏幕快照 2019-03-19 上午11.26.05.png

角对齐组合 相对于superview某一个角组合排布
view.groupInCorner(group: .horizontal, views: [view1, view2, view3], inCorner: .bottomLeft, padding: 10, width: size, height: size)
view.groupInCorner(group: .vertical, views: [view1, view2, view3], inCorner: .bottomRight, padding: 10, width: size, height: size)

屏幕快照 2019-03-19 上午11.28.49.png

边对齐组合,相对于superview某一条边的中点排布
view.groupAgainstEdge(group: .horizontal, views: [view1, view2, view3], againstEdge: .left, padding: 10, width: size, height: size)
view.groupAgainstEdge(group: .vertical, views: [view1, view2, view3], againstEdge: .bottom, padding: 10, width: size, height: size)

Simulator Screen Shot - iPhone XS Max - 2019-03-19 at 11.34.19.png

0FC4A788F2D326B5CFD19EDB76E96ADE.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值