ConstrainTo
ConstrainTo 是对 UIView
的简单扩展,旨在通过不引入新范式来简化 AutoLayout 的常见用例。
ConstrainTo 通过以下方式减少了大量样板代码:
- 自动将
translatesAutoresizingMaskIntoConstraints
设置为false
在被约束的视图中 - 自动激活创建的约束,通过将
isActive
设置为true
- 返回所有创建的约束,以防将来需要访问它们
ConstrainTo 有 2 个主要方法
@discardableResult public func constrain(_ attribute: NSLayoutAttribute, being relation: NSLayoutRelation = .equal, to viewAttribute: NSLayoutAttribute, of view: UIView, multipliedBy multiplier: CGFloat = 1.0, offsetBy offset: CGFloat = 0.0, activate: Bool = true, priority: Float = UILayoutPriorityRequired) -> NSLayoutConstraint
@discardableResult public func constrain(_ attribute: NSLayoutAttribute, being relation: NSLayoutRelation = .equal, to constant: CGFloat, activate: Bool = true, priority: Float = UILayoutPriorityRequired) -> NSLayoutConstraint
除了 7 个便利方法外
@discardableResult public func constrain(to attribute: NSLayoutAttribute, of view: UIView, multipliedBy multiplier: CGFloat = 1.0, offsetBy offset: CGFloat = 0.0) -> NSLayoutConstraint
@discardableResult public func constrain(to size: CGSize) -> (width: NSLayoutConstraintConstraint, heightConstraint: NSLayoutConstraint)
@discardableResult public func constrain(to view: UIView, insetBy insets: UIEdgeInsets = UIEdgeInsets.zero) -> (topConstraint: NSLayoutConstraint, leftConstraint: NSLayoutConstraint, bottomConstraint: NSLayoutConstraint, rightConstraint: NSLayoutConstraint)
@discardableResult public func constrain(toEdgesOf view: UIView, insetBy insets: UIEdgeInsets = UIEdgeInsets.zero) -> (topConstraint: NSLayoutConstraint, leadingConstraint: NSLayoutConstraint, bottomConstraint: NSLayoutConstraint, trailingConstraint: NSLayoutConstraint)
@discardableResult public func constrain(toMarginsOf view: UIView, insetBy insets: UIEdgeInsets = UIEdgeInsets.zero) -> (topMarginConstraint: NSLayoutConstraint, leadingMarginConstraint: NSLayoutConstraint, bottomMarginConstraint: NSLayoutConstraint, trailingMarginConstraint: NSLayoutConstraint)
@discardableResult public func constrain(toCenterOf view: UIView, offsetBy offsets: CGPoint = CGPoint.zero) -> (xConstraint: NSLayoutConstraint, yConstraint: NSLayoutConstraint)
@discardableResult public func constrain(toSizeOf view: UIView) -> (widthConstraint: NSLayoutConstraint, heightConstraint: NSLayoutConstraint)
示例
将属性约束到另一个视图的属性上
如果您想将 redView
的左边与 blueView
的右边对齐,您需要编写以下代码:
redView.constrain(.left, being: .equal, to: .right, of: blueView, multipliedBy: 1, offsetBy: 0, activate: true, priority: UILayoutPriorityRequired)
但是由于默认参数,您只需编写以下内容:
redView.constrain(.left, to: .right, of: blueView)
如果您想将 redView
的左边与 blueView
的右边相距 10 点,您需要编写以下代码:
redView.constrain(.left, to: .right, of: blueView, offsetBy: 10)
约束一个属性
如果您想将 redView
的宽度约束为 20 点,您需要编写以下代码
redView.constrain(.width, being: .equal, to: 20, activate: true, priority: UILayoutPriorityRequired)
但是由于默认参数,您只需编写以下内容:
redView.constrain(.width, to: 20)
将视图约束到另一个视图的同属性
如果您想将 redView
的宽度约束为 blueView
的宽度,您将需要编写以下代码
redView.constrain(to: .width, of: blueView, multipliedBy: 1, offsetBy: 0)
但是由于默认参数,您只需编写以下内容:
redView.constrain(to: .width, of: blueView)
约束大小
如果您想将 redView
的大小约束为 20 x 20 点,您需要编写以下代码
redView.constrain(to: CGSize(width: 20, height: 20))
约束到另一个视图的框架
如果您想将 redView
的框架(顶部、左侧、底部和右侧)约束为 blueView
的框架,您需要编写以下代码
redView.constrain(to: blueView, insetBy: UIEdgeInsets.zero)
但是由于默认参数,您只需编写以下内容:
redView.constrain(to: blueView)
如果您想将 redView
的框架约束为与 blueView
的所有边距均为 10 点内缩,您需要编写以下代码
redView.constrain(to: blueView, insetBy: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
约束到另一个视图的边缘
如果您想将 redView
的边缘(顶部、前边、底部和尾部)约束为 blueView
的边缘,您将需要编写以下代码
redView.constrain(toEdgesOf: blueView, insetBy: UIEdgeInsets.zero)
但是由于默认参数,您只需编写以下内容:
redView.constrain(toEdgesOf: blueView)
如果您想将 redView
的边缘约束为与 blueView
的所有边缘均为 10 点内缩,您将需要编写以下代码
redView.constrain(toEdgesOf: blueView, insetBy: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
限制到另一个视图的边距
如果您想限制 redView
的边距(顶部边距、行首边距、底部边距和行尾边距)为 blueView
的边距,则需要编写:
redView.constrain(toMarginsOf: blueView, insetBy: UIEdgeInsets.zero)
但是由于默认参数,您只需编写以下内容:
redView.constrain(toMarginsOf: blueView)
如果您想将 redView
的边距设置为向所有边距内缩进 10 点到 blueView
,则需编写:
redView.constrain(toMarginsOf: blueView, insetBy: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
限制到另一个视图的中心
如果您想使 redView
在 blueView
中居中,则需要编写:
redView.constrain(toCenterOf: blueView, offsetBy: CGPoint.zero)
但是由于默认参数,您只需编写以下内容:
redView.constrain(toCenterOf: blueView)
如果您想使 redView
的中心位于 blueView
中心的下方 10 点处,则需要编写:
redView.constrain(toCenterOf: blueView, offsetBy: CGPoint(x: 0, y: 10))
限制到另一个视图的大小
如果您想将 redView
的大小限制为与 blueView
的大小相同,则需要编写:
redView.constrain(toSizeOf: blueView)