SuperLayout 是一个库,为 Swift 添加了一些自定义操作符,使得使用 Auto Layout 的 NSLayoutAnchor API变得非常简单。SuperLayout 不会覆盖已在 Equatable
中定义的方法(如 ==
和 >=
),而是定义了逻辑性强且易于理解的方法,以便让可能继承您的代码库或加入您的团队的任何人都能轻松理解。
功能介绍
简而言之,SuperLayout 可以将以下内容转换为:
转换为以下内容
工作原理
SuperLayout 定义了三个自定义操作符:~~
、≥≥
和 ≤≤
,分别对应于 equalTo
、≥
对应于 greaterThanOrEqualTo
、≤
对应于 lessThanOrEqualTo
。
对于大小比较操作符的选择考虑了实用性;《code>≥ 和 ≤
在 macOS 中具有简单的键盘快捷键(只需按下 Option
+ <
和 Option
+ >
),因此编写约束时无需复制粘贴字符。
安装
SuperLayout 通过 CocoaPods 和 SwiftPM 提供。如果您正在使用 CocoaPods,只需在 Podfile 中指定即可。
pod 'SuperLayout'
使用前
要使用这个库,你应该对 NSLayoutAnchor
API 有基本了解。如果不知道,请查看以下文档并开始学习。
NSLayoutConstraint 参考
注意:SuperLayout 目前(尚)不能(自动)关闭你想要使用 Auto Layout 管理的视图的
translatesAutoresizingMaskIntoConstraints
。自动禁用这个设置对我来说太神奇了。如果你不同意,请随时创建一个问题。
constraint(equalTo:) ➥ viewA.rightAnchor ~~ viewB.leftAnchor
原始
viewA.rightAnchor.constraint(equalTo: viewB.leftAnchor).isActive = true
constraint(equalTo:constant:) ➥ viewA.rightAnchor ~~ viewB.leftAnchor + C
原始
viewA.rightAnchor.constraint(equalTo: viewB.leftAnchor, constant: C).isActive = true
constraint(greaterThanOrEqualTo:) ➥ viewA.rightAnchor ≥≥ viewB.leftAnchor
原始
viewA.rightAnchor.constraint(greaterThanOrEqualTo: viewB.leftAnchor).isActive = true
constraint(greaterThanOrEqualTo:constant:) ➥ viewA.rightAnchor ≥≥ viewB.leftAnchor + C
原始
viewA.rightAnchor.constraint(greaterThanOrEqualTo: viewB.leftAnchor, constant: C).isActive = true
constraint(lessThanOrEqualTo:) ➥ viewA.rightAnchor ≤≤ viewB.leftAnchor
原始
viewA.rightAnchor.constraint(lessThanOrEqualTo: viewB.leftAnchor).isActive = true
constraint(lessThanOrEqualTo:constant:) ➥ viewA.rightAnchor ≤≤ viewB.leftAnchor + C
原始
viewA.rightAnchor.constraint(lessThanOrEqualTo: viewB.leftAnchor, constant: C).isActive = true
NSLayoutDimension 参考
constraint(equalTo:multiplier:) ➥ viewA.heightAnchor ~~ viewB.heightAnchor * M
原始
viewA.heightAnchor.constraint(equalTo: viewB.heightAnchor, multiplier: M).isActive = true
constraint(equalTo:multiplier:constant:) ➥ viewA.heightAnchor ~~ viewB.heightAnchor * M + C
原始
viewA.heightAnchor.constraint(equalTo: viewB.heightAnchor, multiplier: M, constant: C).isActive = true
constraint(equalToConstant:) ➥ viewA.heightAnchor ~~ C
原始
viewA.heightAnchor.constraint(equalToConstant: C).isActive = true
constraint(greaterThanOrEqualTo:multiplier:) ➥ viewA.heightAnchor ≥≥ viewB.heightAnchor * M
原始
viewA.heightAnchor.constraint(greaterThanOrEqualTo: viewB.heightAnchor, multiplier: M).isActive = true
constraint(greaterThanOrEqualTo:multiplier:constant:) ➥ viewA.heightAnchor ≥≥ viewB.heightAnchor * M + C
原始
viewA.heightAnchor.constraint(greaterThanOrEqualTo: viewB.heightAnchor, multiplier: M, constant: C).isActive = true
constraint(greaterThanOrEqualToConstant:) ➥ viewA.heightAnchor ≥≥ C
原始
viewA.heightAnchor.constraint(greaterThanOrEqualToConstant: C).isActive = true
constraint(lessThanOrEqualTo:multiplier:) ➥ viewA.heightAnchor ≤≤ viewB.heightAnchor * M
原始
viewA.heightAnchor.constraint(lessThanOrEqualTo: viewB.heightAnchor, multiplier: M).isActive = true
约束(lessThanOrEqualTo:multiplier:constant:) ➥ viewA.heightAnchor ≤≤ viewB.heightAnchor * M + C
原始
viewA.heightAnchor.constraint(lessThanOrEqualTo: viewB.heightAnchor, multiplier: M, constant: C).isActive = true
约束(lessThanOrEqualToConstant:) ➥ viewA.heightAnchor ≤≤ C
原始
viewA.heightAnchor.constraint(lessThanOrEqualToConstant: C).isActive = true
许可证
SuperLayout 在 Apache 2.0 许可证下可用。更多信息请参阅 LICENSE。