-
Notifications
You must be signed in to change notification settings - Fork 10
/
SelfConstraintBuilder.swift
182 lines (162 loc) · 9 KB
/
SelfConstraintBuilder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// SelfConstraintBuilder.swift
// SwiftAutoLayout
//
// Created by Jake Sawyer on 10/23/18.
// Copyright © 2018 SwiftKick Mobile. All rights reserved.
//
// Look at all this code you didn't have to write!
import Foundation
/**
Creates constraints that are self-defining, like width, height, and aspect ratios.
*/
public class SelfConstraintBuilder: ConstraintBuilder {
/// The constraints created by this builder, in the order they were made.
public internal(set) var constraints = [NSLayoutConstraint]()
let constrainable: Constrainable
/**
Views will have their `translatesAutoresizingMaskIntoConstraints` disabled.
*/
init(for constrainable: Constrainable) {
self.constrainable = constrainable
if let view = constrainable as? UIView {
view.translatesAutoresizingMaskIntoConstraints = false
}
}
/// Helper method for defining self constraints instead of constraints between two `Constrainable`s.
func makeDimensionConstraint(anchor: NSLayoutDimension,
relation: NSLayoutConstraint.Relation,
constant rawConstant: CGFloat,
priority: UILayoutPriority,
activate: Bool) -> NSLayoutConstraint {
var constant = rawConstant
if #available(iOS 11, *), constant == .systemSpacing {
assertionFailure("[SwiftAutoLayout] Unsupported usage of `CGFloat.systemSpacing` with `SelfConstraintBuilder`!")
constant = 8
print("[SwiftAutoLayout] Using `\(constant)` as a placeholder value instead of `.systemSpacing` in `SelfConstraintBuilder`!")
}
let result: NSLayoutConstraint
switch relation {
case .equal:
result = anchor.constraint(equalToConstant: constant)
case .lessThanOrEqual:
result = anchor.constraint(lessThanOrEqualToConstant: constant)
case .greaterThanOrEqual:
result = anchor.constraint(greaterThanOrEqualToConstant: constant)
}
result.priority = priority
if activate {
result.isActive = true
}
return result
}
}
// MARK: - Make Constraints
public extension SelfConstraintBuilder {
/**
Constrains width anchor to a constant.
- NOTE: Creates a single constraint.
- Parameters:
- relation: The relationship between the anchors. Defaults to `.equal`
- constant: The constant for the constraint. Defaults to 0
- priority: The priority for the constraint. Defaults to `.required`. Note that `.required` constraints must be activated and cannot be deactivated. Specify a lower value for dynamically activated constraints.
- activate: Whether or not this constraint should be activated now. Disable this if you want to activate the constraint later.
- Returns: The constraint builder. Access any constraints built so far in the order declared via its `constraints` property.
*/
@discardableResult
public func width(_ relation: NSLayoutConstraint.Relation = .equal,
constant: CGFloat = 0,
priority: UILayoutPriority = .required,
activate: Bool = true) -> SelfConstraintBuilder {
let constraint = makeDimensionConstraint(anchor: constrainable.widthAnchor,
relation: relation,
constant: constant,
priority: priority,
activate: activate)
constraints.append(constraint)
return self
}
/**
Constrains height anchor to a constant.
- NOTE: Creates a single constraint.
- Parameters:
- relation: The relationship between the anchors. Defaults to `.equal`
- constant: The constant for the constraint. Defaults to 0
- priority: The priority for the constraint. Defaults to `.required`. Note that `.required` constraints must be activated and cannot be deactivated. Specify a lower value for dynamically activated constraints.
- activate: Whether or not this constraint should be activated now. Disable this if you want to activate the constraint later.
- Returns: The constraint builder. Access any constraints built so far in the order declared via its `constraints` property.
*/
@discardableResult
public func height(_ relation: NSLayoutConstraint.Relation = .equal,
constant: CGFloat = 0,
priority: UILayoutPriority = .required,
activate: Bool = true) -> SelfConstraintBuilder {
let constraint = makeDimensionConstraint(anchor: constrainable.heightAnchor,
relation: relation,
constant: constant,
priority: priority,
activate: activate)
constraints.append(constraint)
return self
}
/**
Constrains width and height anchors individually to the same constant.
- NOTE: Creates two constraints.
- Parameters:
- relation: The relationship between the anchors. Defaults to `.equal`
- constant: The constant for the constraint. Defaults to 0
- priority: The priority for the constraint. Defaults to `.required`. Note that `.required` constraints must be activated and cannot be deactivated. Specify a lower value for dynamically activated constraints.
- activate: Whether or not this constraint should be activated now. Disable this if you want to activate the constraint later.
- Returns: The constraint builder. Access any constraints built so far in the order declared via its `constraints` property.
*/
@discardableResult
public func widthHeight(_ relation: NSLayoutConstraint.Relation = .equal,
constant: CGFloat = 0,
priority: UILayoutPriority = .required,
activate: Bool = true) -> SelfConstraintBuilder {
width(relation, constant: constant, priority: priority, activate: activate)
height(relation, constant: constant, priority: priority, activate: activate)
return self
}
/**
Constrains width and height anchors individually to the CGSize's width and height respectively.
- NOTE: Creates two constraints.
- Parameters:
- relation: The relationship between the anchors. Defaults to `.equal`
- constant: The constant for the constraint. Defaults to 0
- priority: The priority for the constraint. Defaults to `.required`. Note that `.required` constraints must be activated and cannot be deactivated. Specify a lower value for dynamically activated constraints.
- activate: Whether or not this constraint should be activated now. Disable this if you want to activate the constraint later.
- Returns: The constraint builder. Access any constraints built so far in the order declared via its `constraints` property.
*/
@discardableResult
public func widthHeight(_ relation: NSLayoutConstraint.Relation = .equal,
size: CGSize = .zero,
priority: UILayoutPriority = .required,
activate: Bool = true) -> SelfConstraintBuilder {
width(relation, constant: size.width, priority: priority, activate: activate)
height(relation, constant: size.height, priority: priority, activate: activate)
return self
}
/**
Constrains the height anchor to the width anchor with an aspect ratio multiplier.
- NOTE: Creates a single constraint.
- Parameters:
- relation: The relationship between the anchors. Defaults to `.equal`
- constant: The constant for the constraint. Defaults to 0
- priority: The priority for the constraint. Defaults to `.required`. Note that `.required` constraints must be activated and cannot be deactivated. Specify a lower value for dynamically activated constraints.
- activate: Whether or not this constraint should be activated now. Disable this if you want to activate the constraint later.
- Returns: The constraint builder. Access any constraints built so far in the order declared via its `constraints` property.
*/
@discardableResult
public func aspectRatio(_ aspectRatio: CGFloat,
priority: UILayoutPriority = .required,
activate: Bool = true) -> SelfConstraintBuilder {
let constraint = constrainable.heightAnchor.constraint(equalTo: constrainable.widthAnchor, multiplier: aspectRatio)
constraint.priority = priority
if activate {
constraint.isActive = true
}
constraints.append(constraint)
return self
}
}