EZConstraints 提供了一种更简单、更具描述性的方式来编写自动布局约束。
特性
- 支持 Swift 5。
- 使用简化有意义语法进行自动布局。
- 创建时设置约束属性,如关系、优先级。
- 约束默认为活动状态。
- 可选存储约束。
- 允许视图获取另一个视图的布局属性,如大小、居中、锚点。
- 一次调用为视图组分配一个布局约束。
- 无需设置 'translatesAutoresizingMaskIntoConstraints'。
- 使用有意义标识符具有更好的调试体验。
要求
- iOS 10.0+
- Xcode 11+
- Swift 5.1+
安装
CocoaPods 是 Cocoa 项目的依赖项管理器。有关使用和安装说明,请访问他们的网站。要将 EZConstraints 集成到您的 Xcode 项目中,请使用 CocoaPods,在您的 Podfile
中指定它
pod 'EZConstraints'
基础
类型别名
EZConstraints
提供了更易于阅读的类型别名来描述约束。
EZConstraint
=UILayoutConstraint
EZConstraints
=[UILayoutConstraint]
EZInsets
=UIEdgeInsets
填充父视图
使用 NSAutoLayout
填充父视图
view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: superview.topAnchor, constant: 0),
view.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 0),
view.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: 0),
view.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: 0)
])
使用 EZConstraints
view.edgesToSuperView()
在父视图中居中
使用 NSAutoLayout
在父视图中居中视图
view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
view.centerXAnchor.constraint(equalTo: superview.centerXAnchor),
view.centerYAnchor.constraint(equalTo: superview.centerYAnchor)
])
使用 EZConstraints
view.center()
或
view.center(offset: CGPoint(x:20, y:10))
使用方法
填充父视图
填充父视图
view.edgesToSuperView()
您还可以指定边缘和内边距
view.edgesToSuperView(including: [.left, .right], insets: .left(8) + .right(8))
EZConstraints 还支持使用 edgesToSuperviewSafeArea(including:, insets:)
使用安全区域内边距。
在父视图中布局
将视图放在父视图之上
view.layTopInSuperView(constant: 20)
您还可以使用 layLeftInSuperView(constant:)
、layBottomInSuperView(constant:)
和 layRightInSuperView(constant:)
。
布局
将视图放置在另一个视图下方
view.layBelow(view2, constant: 16)
您还可以使用layAbove(_:)
、layRight(to:)
和layLeft(to:)
。
EZConstraints还允许您将视图的边距排列到安全区域。
view.layTopToSafeArea(constant: 16)
您还可以使用layLeftToSafeArea(constant:)
、layBottomToSafeArea(constant:)
和layRightToSafeArea(constant:)
。
请注意,调用edgesToSuperView
具有与以下相同的效果
view.layTopInSuperView(constant: 0)
view.layLeftInSuperView(constant: 0)
view.layBottomInSuperView(constant: 0)
view.layRightInSuperView(constant: 0)
同样适用于edgesToSuperviewSafeArea
。
居中
EZConstraints支持水平和垂直居中
view.centerHorizontally()
view.centerVertically(yOffset: 16)
您还可以将视图相对于父视图的中心居中
view.center()
元素尺寸
分配一个由宽度和高度约束组成的尺寸约束
view.sizeAnchor(CGSize(width: 30, height: 30))
分配一个固定宽度
view.widthAnchor(.equal, constant: 100)
您还可以将其他视图的尺寸、高度、宽度约束分配给指定视图
view.heightAnchor(with: view2)
您还可以使用heightAnchor(with:)
和widthAnchor(with:)
进行相同操作。squareSizeWith(sideLengthOf:)
将视图的边长设置为所有边相等的正方形。
分配宽高比约束
view.aspectRatio(multiplier: 1.5) // width = height * 1.5
在两个视图之间分配宽高比约束
view.aspectRatio(by: view2, multiplier: 0.5) // view.width = view2.height * 0.5
调试标识符
调试标识符是创建约束时自动分配的唯一标识符。此标识符通过消除歧义并使您更容易了解问题的原因,从而有助于调试冲突的约束。该标识符具有特定的格式: id : <View1 Address>.Function_Name relation <View2 Address> * multiplier + constant(pt)
不会出现在标识符中的属性
<View2 Address>
,如果在例如'widthAnchor(_:, constant:)'中为nil
。multiplier
,如果multiplier == 1.0
。constant
,如果constant == 0
。- 其他诸如
priority
的属性。
示例
id : 0x000000000000.layoutLeftInSuperView = 0x111111111111 + 24.0pt
id : 0x000000000000.widthAnchor = 100.0pt'
许可证
EZConstraints遵循MIT许可证发布。 查看LICENSE了解详细信息。
作者
贡献
如果您有功能请求或错误报告,欢迎发送Pull Requests或通过 创建新问题 帮助。