约束 0.9.5

Constraint 0.9.5

Lucas van Dongen 维护。



Constraint

Constraint 是 iOS Auto Layout 的简单包装器,具有非常自然的语法

CI Status Version License Platform

使用方法

在 UIView 上

通常,您会使用 Constraint 库对您想要约束的 (descendant class of) UIView 实例进行操作。每次调用都返回 UIView,因此可以轻松地将所有布局链接起来,例如

icon
    .attach(top: 20,
            leading: 10,
            bottom: 0,
            trailing: 10)
    .size(width: 24, height: 24)

还可以添加各种修饰符来偏移约束

image.attach(top: 0.orMore, bottom: 12.defaultLowPrioritized)
// These are also chainable
label.attach(bottom: 0.orMore.prioritized(to: UILayoutPriority(800))

如果您想要保存某个 Offset 以供重复使用,可以这样做

let offset = 0.orMore.defaultLowPrioritized.respectingLayoutGuides

可以通过使用 respectingLayoutGuide / layoutGuideRespecting 来尊重任何布局指南(如 iPhone X 的安全区域)

在一组 UIView 上

还可以使用视图数组。例如,现在可以在某个方向上对数组进行空间分布,或将它们全部附加到父视图中。最常用的功能是将它们简单地作为子视图添加

[view1, view2, view3].addedAsSubviews(to: superView).attach()

如您所见,此方法也是流畅的。

作为约束的生成器

有时你需要存储生成的约束。在这种情况下,你需要直接调用类Constraint的静态方法,如下所示

private var messageHeight: NSLayoutContraint?
// ...
messageHeight = Constraint.height(50, for: message).activated

在这种情况下,你必须手动将约束添加到视图中。这是一种最大的灵活性方式,但同时需要更多的操作。

UIView API

这些都是对UIView公开的扩展。它们基于在Interface Builder中看到的选择。以下是对它们之间关系的简要概述

Align Screen

  • align()函数对应于两个相等视图之间关系的第一个7个选项
  • center()函数用于对齐屏幕的最后两个选项,其中子视图在其父视图superview中居中

Size Constraints

  • Spacing to nearest neighbor由所有attach()函数覆盖
  • WidthHeightEqual WidthsEqual Heightswidth()height()size()覆盖
  • Aspect Ratioratio()函数处理
  • Align是Align屏幕中边缘的别称,因此由align覆盖

attach

这个方法用于在相关的视图和superview之间确定间距。你可以同时定义所有侧面,分别定义每个侧面,也可以省略一些侧面。它以Offsetable作为参数,这意味着你可以使用原始数据类型如IntCGFloat,或者直接发送一个Offset

一次性连接所有侧面

这是使用attach的最基本方法。你可以定义一个offset或接受默认值0

view.attach() 
view.attach(offset: 12)

仅附加某些边

此版本允许您指定要附加哪些边。您可以定义一个偏移量或接受默认值0

view.attach(sides: [.top, .leading, .trailing], 12)

按边分配不同的值

这是使用此API的最灵活方法。每个边都可以有自己的单独定义,它是可变的,因此可以在需要时进一步修改。

view.attach(top: 0, trailing: 12) // Does not apply the bottom and leading constraints
view.attach(top: 0.orMore) // It's possible to use it with primitives and still modify the priority or relation type
view.attach(leading: 12.orLess.defaultLowPriority) // These can also be chained
view.attach(bottom: Offset(0, .orMore, respectingLayoutGuide: true, priority: .defaultLow)) // Means the same as view.attach(bottom: 0.orMore.layoutGuideRespecting.defaultLowPriority)

respectingLayoutGuide / layoutGuideRespecting意味着它尊重布局指导线,例如根视图中,您希望它在某些时候尊重安全区域。

默认情况下,不尊重布局指导线。

中心对齐

中心对齐允许您在一个视图内部居中一个视图,默认为superview。也可以指定另一个需要成为同一视图树一部分的视图。

view.center(axis: .both) // Centers the view on both the X and Y axis of it's superview
view.center(axis: .x, adjusted: 10, priority: .defaultLow) // Wants to center it's X to it's superview, then adjusts it +10 pixels and applies a low priority to it

对齐

当您希望对齐两个不在父子关系中时,使用对齐。

相互对齐视图

类似于为父子视图提供的center() API,中心也可以对齐。

view.align(axis: .x, to: anotherView, adjustment: 10) // Wants to center it's X to anotherView, then adjusts it +10 pixels

对视图边界的对齐

它还允许您对齐一边而不是中间

view.align(.leading, 12, otherView) // Aligns it's leading side to the leading side of otherView + 12 pixels

如果需要对齐多个边(类似于attach),也可以这样做

view.align([.top, .leading, .bottom], 0, to: otherView)

space

在任意方向上间距其他视图。

registerButton
    .space(20, .above, reconfirmButton)
    .space(8, .below, usernameLabel, .orMore, priority: .defaultLow)

widthheight以及size

这些函数用于设置UIView的大小。您还可以设置与其他视图的宽度和高度相关的宽度和高度。

将宽度、高度和大小设置为常量

otherView
    .size(width: 100, .orMore, height: 50)
view
    .width(200)
    .height(100)

size()也接受一个参数CGRect,如果您需要复制frame.size,则非常有用

view.size(superview.frame.size)

将宽度或高度设置为与另一个视图相关

您也可以将其设置为与另一个视图相关

view.height(relatedTo: superview, adjusted: 10)

ratio

比例设置视图宽度和高度之间的比例。

view.ratio(of: 2) // Makes the width twice as much as the height
view.ratio(of: 3, to: 2) // Makes the width height have a ratio of 3:2

或者,您也可以使用一个CGRect来调用它。如果想要确保UIImageView的显示比例始终与UIImage相同,这会非常有用

view.ratio(avatarImage.size)

已知问题与待办事项

这是本库的0.1版本,但已经在几个内部项目中使用,并且已经解决了所有主要问题。以下列出存在的问题:

  • 并非所有Constraint类中的函数都返回NSLayoutConstraint[NSLayoutConstraint]
  • Fluent API还没有在所有地方使用。
  • API可能需要进行一些命名更改或改进参数。

示例

要运行示例项目,请先克隆仓库,然后从示例目录运行pod install

要求

  • Swift 4
  • iOS

安装

Constraint可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中

pod 'Constraint'

作者

lucasvandongen,[email protected]

许可证

Constraint基于MIT许可证提供。有关更多信息,请参阅LICENSE文件。