SimpleConstraints

Swift 5.9 iOS v12+ tvOS v12+ Release License: MIT

SimpleConstraints

SimpleConstraints 是一个 Swift 库,旨在简化在 UIKit 中创建和管理约束的过程。它提供了一组实用工具,使在 iOS 应用程序中对齐和定位 UI 元素变得更加容易。

功能特性

预览

让我们使用常规 UIKit 约束向视图添加一个具有以下属性的图像

使用原生 UIKit 的约束

view.addSubview(image)
image.topAnchor.constraint(equalTo: view.topAnchor),
image.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
image.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
image.heightAnchor.constraint(equalTo: image.widthAnchor, multiplier: 1/1)

使用 SimpleConstraints 的约束

view.edgesAndRatio(image, top: nil, left: nil, right: nil, aspectRatio: 1)

安装

SimpleConstraints 可通过 Swift Package Manager 获得。要安装,请选择 File > Swift Packages > Add Package Dependency ... 并添加 SimpleConstraints 仓库 URL

https://github.com/nicolaischneider/SimpleConstraints.git

用法:安全约束

SDK 涵盖了 6 种不同的约束情况,以确保在一个函数中设置所有约束

  1. 4 条边(上、左、右、下)
  2. 3 条边,一个长度(高度或宽度)
  3. 2 条边,2 个长度(高度和宽度)
  4. 3 条边和宽高比(高度或宽度)
  5. 高度、宽度、中心 x、中心 y
  6. 1 条边,中心 x 或 y,高度和宽度

约束参数

ConstraintYAnchor/ConstraintXAnchor 枚举允许您指定顶部、底部、左侧和右侧约束,并带有可选的常量用于间距和额外的用例(例如 .topSafe.rightSafe、...)以确定是否应考虑安全区域。case 描述了第一个参数中给定视图的锚点。第二个参数设置内边距。

.top(view, 10)

在以下示例中,我们子视图的顶部锚点固定到 otherView 的底部锚点,内边距为 20。当 ConstraintYAnchorConstraintXAnchor 设置为 nil 时,SimpleConstraints 将自动使用相应的父视图约束 - left: nil 等效于 left: .left(view, 0)

view.edges(subView, top: .bottom(otherView, 20), bottom: nil, left: nil, right: nil)

ConstraintXCenterConstraintYCenter 的工作方式类似,并描述了 centerXAnchorcenterYAnchor

案例 1:所有边

给定四条边(上、左、右、下),edges() 函数可以轻松设置所有需要的约束。以下视图与父视图的顶部安全区域有 20 的内边距,与底部有 10 的内边距。左右锚点相同。

view.edges(subView, top: .topSafe(view, 20), bottom: .bottom(view, 10), left: nil, right: nil)

案例 2:3 条边和一个长度(高度/宽度)

当给定视图的三条边和一个长度时,使用 edgesAndHeight()。在以下情况下,高度设置为 20,而顶部、左侧和右侧边固定到父视图的约束。

view.edgesAndHeight(subView, top: nil, left: nil, right: nil, height: 20)

此案例的替代函数

案例 3:2 条边、高度和宽度

当给定视图的两个相邻边、长度和高度时,使用 edgesAndSize()。在以下情况下,对象的宽度和高度均设置为 30,并固定到父视图的左上角。

view.edgesAndSize(subView, top: nil, left: nil, height: 30, width: 30)

案例 4:3 条边和宽高比

当给定视图的三条边以及缺失的高度/宽度的宽高比时,使用 edgesAndRatio()。在以下情况下,视图固定到视图的顶部、左侧和右侧,宽高比为 1:1。

view.edgesAndRatio(subView, top: nil, left: nil, right: nil, aspectRatio: 1)

案例 5:中心 X 和中心 Y 以及给定的尺寸

当给定 y 轴和 x 轴的中心,以及完整尺寸(高度+宽度)时,使用 centerWithSize()。在以下情况下,给定视图设置为 otherView1 的 x 中心,内边距为 20,以及 otherView2 的 y 中心,内边距为 -10。尺寸为 30x30。

view.centerWithSize(subView, 
                    centerX: .centerX(otherView1, 20), 
                    centerY: .centerY(otherView2, -10),
                    height: 30, 
                    width: 30)

案例 6:1 条边、1 个中心轴和给定的尺寸

当给定视图的一条边、x 或 y 的中心轴和尺寸时,使用 ...CenterWithSize()。在以下情况下,视图固定到顶部,并在父视图的 x 轴上居中,尺寸为 30x30。

view.topCenterWithSize(subView, top: nil, centerX: .centerX(view, 0), height: 30, width: 30)

此案例的现有函数

用法:不安全约束

或者,SimpleConstraints 也支持不安全地设置约束。这可以通过使用 unsafeConstraints() 传递 Straint 数组来完成。所有约束将按照给定的确切顺序处理。此函数保证对象的显示,因为未设置任何限制。

Straint

一个 Straint 由以下部分组成

不安全约束的示例

view.unsafeConstraints(subview, constraints: [
    Straint(straint: .top, anchor: .bottom(otherView, 40)),
    Straint(straint: .left, anchor: .left(view, 20)),
    Straint(straint: .right, anchor: .right(view, -20)),
    Straint(straint: .height, anchor: .length(30))
])

“翻译”成常规的自动布局实现,代码如下所示

view.addSubview(subview)
subView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    subView.topAnchor.constraint(equalTo: otherView.bottomAnchor, constant: 40),
    subView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20),
    subView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20),
    subView.heightAnchor.constraint(equalToConstant: 30)
])

用法辅助函数

启用/禁用约束

您可以使用 enableConstraint / disableConstraint 启用/禁用给定锚点的约束。相应的锚点作为参数传递。

view.disableConstraint(for: .top)

移除所有约束

使用 removeAllConstraints() 移除所有约束。

贡献

如果您发现任何问题或有建议,请创建 issue。