自动布局第三方Neon的基础使用

来源:互联网 发布:linux file命令 x86 编辑:程序博客网 时间:2024/05/01 03:14

Neon

参考自github

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


居中

设定 view 在 superview 的中心,只要调用 anchorInCenter

view1.anchorInCenter(width: size, height: size)

swiftcn-Neon-anchorincenter

填充

有时候如果想 view 填充父类,可以调用 fillSuperview

view.fillSuperview()or view1.fillSuperview(left: padding, right: padding, top: padding, bottom: padding)

swiftcn-Neon-fillsuperview

角对齐

如果想 view 相对于 superview 固定于某个角,例如右上角,可以尝试用anchorInCorner,并传入要对齐哪个角的参数

view1.anchorInCorner(.TopLeft, xPad: padding, yPad: padding, width: size, height: size)view2.anchorInCorner(.TopRight, xPad: padding, yPad: padding, width: size, height: size)view3.anchorInCorner(.BottomLeft, xPad: padding, yPad: padding, width: size, height: size)view4.anchorInCorner(.BottomRight, xPad: padding, yPad: padding, width: size, height: size)

swiftcn-Neon-anchorInCorner

边对齐

如果想让 view 相对于 superview 的某一边依靠对齐,可以用anchorToEdge,例如:

view1.anchorToEdge(.Top, padding: padding, width: size, height: size)view2.anchorToEdge(.Left, padding: padding, width: size, height: size)view3.anchorToEdge(.Bottom, padding: padding, width: size, height: size)view4.anchorToEdge(.Right, padding: padding, width: size, height: size)

swiftcn-Neon-anchorToEdge

边填充

如果想让 view 相对于 superview 的某一边对齐并填拉伸充,可以用anchorAndFillEdge,对应代码

view1.anchorAndFillEdge(.Top, xPad: padding, yPad: padding, otherSize: size)view2.anchorAndFillEdge(.Bottom, xPad: padding, yPad: padding, otherSize: size)view3.anchorAndFillEdge(.Left, xPad: padding, yPad: padding, otherSize: size)view4.anchorAndFillEdge(.Right, xPad: padding, yPad: padding, otherSize: size)

swiftcn-Neon-anchorAndFillEdge

相对对齐

设定 view 相对于目标 anchorView 对齐(不再是superview),可以用align

view1.align(.AboveMatchingLeft, relativeTo: anchorView, padding: padding, width: size, height: size)view2.align(.AboveCentered, relativeTo: anchorView, padding: padding, width: size, height: size)view3.align(.AboveMatchingRight, relativeTo: anchorView, padding: padding, width: size, height: size)view4.align(.ToTheRightMatchingTop, relativeTo: anchorView, padding: padding, width: size, height: size)view5.align(.ToTheRightCentered, relativeTo: anchorView, padding: padding, width: size, height: size)view6.align(.ToTheRightMatchingBottom, relativeTo: anchorView, padding: padding, width: size, height: size)view7.align(.UnderMatchingRight, relativeTo: anchorView, padding: padding, width: size, height: size)view8.align(.UnderCentered, relativeTo: anchorView, padding: padding, width: size, height: size)view9.align(.UnderMatchingLeft, relativeTo: anchorView, padding: padding, width: size, height: size)view10.align(.ToTheLeftMatchingBottom, relativeTo: anchorView, padding: padding, width: size, height: size)view11.align(.ToTheLeftCentered, relativeTo: anchorView, padding: padding, width: size, height: size)view12.align(.ToTheLeftMatchingTop, relativeTo: anchorView, padding: padding, width: size, height: size)

swiftcn-Neon-align

对齐并填充

你不知道或者没法指定一个相对视图的大小,但又要兼顾填充和对齐,并且还依赖与相邻的 view。结合所有前面讨论的不同对齐类型,我们可以定义更加复杂的依赖:

view2.alignAndFillWidth(align: .ToTheRightMatchingTop, relativeTo: view1, padding: padding, height: size / 2.0)view4.alignAndFillHeight(align: .AboveCentered, relativeTo: view3, padding: padding, width: size / 2.0)view6.alignAndFill(align: .ToTheLeftMatchingTop, relativeTo: view5, padding: padding)

swiftcn-Neon-alignAndFillWidth

挤压对齐

如果两个 view 夹着中间一个 view 的需求,可以看看这两方法alignBetweenHorizontalalignBetweenVertical

view1.alignBetweenHorizontal(align: .ToTheRightMatchingTop, primaryView: anchorViewA, secondaryView: anchorViewB, padding: padding, height: size)view2.alignBetweenVertical(align: .UnderCentered, primaryView: anchorViewB, secondaryView: anchorViewD, padding: padding, width: size)view3.alignBetweenHorizontal(align: .ToTheLeftMatchingBottom, primaryView: anchorViewD, secondaryView: anchorViewC, padding: padding, height: size)view4.alignBetweenVertical(align: .AboveMatchingRight, primaryView: anchorViewC, secondaryView: anchorViewA, padding: padding, width: size)

swiftcn-Neon-align_between_fill

组合

组合的 api 有点类似上面地用法,不过是组成一个组后做的相对依赖,不废话,直接贴代码

居中对齐组合

///groupInCenteranchorViewA.groupInCenter(group: .Horizontal, views: [view1, view2, view3], padding: padding, width: size, height: size)anchorViewB.groupInCenter(group: .Vertical, views: [view4, view5, view6], padding: padding, width: size, height: size)

swiftcn-Neon-group_in_center

角对齐组合

//groupInCorneranchorViewA.groupInCorner(group: .Horizontal, views: [view1, view2, view3], inCorner: .TopLeft, padding: padding, width: size, height: size)anchorViewB.groupInCorner(group: .Vertical, views: [view4, view5, view6], inCorner: .BottomRight, padding: padding, width: size, height: size)

swiftcn-Neon-group_in_corner

边对齐组合

//groupAgainstEdgeanchorViewA.groupAgainstEdge(group: .Horizontal, views: [view1, view2, view3], againstEdge: .Left, padding: padding, width: size, height: size)anchorViewB.groupAgainstEdge(group: .Vertical, views: [view4, view5, view6], againstEdge: .Bottom, padding: padding, width: size, height: size)

swiftcn-Neon-group_in_center

0 0
原创粉丝点击