SimpleConstraints 是一个 Swift 库,旨在简化在 UIKit 中创建和管理约束的过程。它提供了一组实用工具,使在 iOS 应用程序中对齐和定位 UI 元素变得更加容易。
translatesAutoresizingMaskIntoConstraints
设置为 false
)让我们使用常规 UIKit 约束向视图添加一个具有以下属性的图像
safeAreaLayoutGuide
固定到顶部。使用原生 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 种不同的约束情况,以确保在一个函数中设置所有约束
ConstraintYAnchor
/ConstraintXAnchor
枚举允许您指定顶部、底部、左侧和右侧约束,并带有可选的常量用于间距和额外的用例(例如 .topSafe
、.rightSafe
、...)以确定是否应考虑安全区域。case 描述了第一个参数中给定视图的锚点。第二个参数设置内边距。
.top(view, 10)
在以下示例中,我们子视图的顶部锚点固定到 otherView
的底部锚点,内边距为 20。当 ConstraintYAnchor
或 ConstraintXAnchor
设置为 nil 时,SimpleConstraints 将自动使用相应的父视图约束 - left: nil
等效于 left: .left(view, 0)
。
view.edges(subView, top: .bottom(otherView, 20), bottom: nil, left: nil, right: nil)
ConstraintXCenter
和 ConstraintYCenter
的工作方式类似,并描述了 centerXAnchor
和 centerYAnchor
。
给定四条边(上、左、右、下),edges()
函数可以轻松设置所有需要的约束。以下视图与父视图的顶部安全区域有 20 的内边距,与底部有 10 的内边距。左右锚点相同。
view.edges(subView, top: .topSafe(view, 20), bottom: .bottom(view, 10), left: nil, right: nil)
当给定视图的三条边和一个长度时,使用 edgesAndHeight()
。在以下情况下,高度设置为 20,而顶部、左侧和右侧边固定到父视图的约束。
view.edgesAndHeight(subView, top: nil, left: nil, right: nil, height: 20)
此案例的替代函数
edgesAndWidth()
edgesAndCenterYWithHeight()
edgesAndCenterXWithWidth()
当给定视图的两个相邻边、长度和高度时,使用 edgesAndSize()
。在以下情况下,对象的宽度和高度均设置为 30,并固定到父视图的左上角。
view.edgesAndSize(subView, top: nil, left: nil, height: 30, width: 30)
当给定视图的三条边以及缺失的高度/宽度的宽高比时,使用 edgesAndRatio()
。在以下情况下,视图固定到视图的顶部、左侧和右侧,宽高比为 1:1。
view.edgesAndRatio(subView, top: nil, left: nil, right: nil, aspectRatio: 1)
当给定 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)
当给定视图的一条边、x 或 y 的中心轴和尺寸时,使用 ...CenterWithSize()
。在以下情况下,视图固定到顶部,并在父视图的 x 轴上居中,尺寸为 30x30。
view.topCenterWithSize(subView, top: nil, centerX: .centerX(view, 0), height: 30, width: 30)
此案例的现有函数
topCenterWithSize()
bottomCenterWithSize()
leftCenterWithSize()
rightCenterWithSize()
或者,SimpleConstraints 也支持不安全地设置约束。这可以通过使用 unsafeConstraints()
传递 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。