@@ -129,40 +129,55 @@ public extension UIView {
129
129
/// - trailing: Constant to apply from trailingAnchor (if nil, not applied)
130
130
/// - width: Constant to apply from widthAnchor (if nil, not applied)
131
131
/// - height: Constant to apply from heightAnchor (if nil, not applied)
132
+ /// - centerX: Boolean determining if centerX should be applied (if nil, not applied)
133
+ /// - centerY: Boolean determining if centerY should be applied (if nil, not applied)
134
+ /// - Note: width and height are not in respect to superview, but always to self
132
135
/// - Returns: ConstraintManager holding all the values associated with constraints
133
136
@discardableResult
134
137
func snap( to view: UIView ? = nil , top: CGFloat ? = nil , leading: CGFloat ? = nil , bottom: CGFloat ? = nil ,
135
138
trailing: CGFloat ? = nil , width: CGFloat ? = nil , height: CGFloat ? = nil , centerX: Bool ? = nil ,
136
139
centerY: Bool ? = nil ) -> ConstraintManager {
137
- guard let view = view ?? superview else { return ConstraintManager ( ) }
138
140
translatesAutoresizingMaskIntoConstraints = false
139
141
var constraintManager = ConstraintManager ( )
142
+ if let width = width {
143
+ constraintManager. width = widthAnchor. constraint ( equalToConstant: width)
144
+ constraintManager. width? . isActive = true
145
+ }
146
+ if let height = height {
147
+ constraintManager. height = heightAnchor. constraint ( equalToConstant: height)
148
+ constraintManager. height? . isActive = true
149
+ }
150
+ guard let view = view ?? superview else { return constraintManager }
140
151
if let top = top {
141
152
constraintManager. top = topAnchor. constraint ( equalTo: view. topAnchor, constant: top)
153
+ constraintManager. top? . isActive = true
142
154
}
143
155
if let leading = leading {
144
156
constraintManager. leading = leadingAnchor. constraint ( equalTo: view. leadingAnchor, constant: leading)
157
+ constraintManager. leading? . isActive = true
145
158
}
146
159
if let bottom = bottom {
147
160
constraintManager. bottom = bottomAnchor. constraint ( equalTo: view. bottomAnchor, constant: bottom)
161
+ constraintManager. bottom? . isActive = true
148
162
}
149
163
if let trailing = trailing {
150
164
constraintManager. trailing = trailingAnchor. constraint ( equalTo: view. trailingAnchor, constant: trailing)
151
- }
152
- if let width = width {
153
- constraintManager. width = widthAnchor. constraint ( equalToConstant: width)
154
- }
155
- if let height = height {
156
- constraintManager. height = heightAnchor. constraint ( equalToConstant: height)
165
+ constraintManager. trailing? . isActive = true
157
166
}
158
167
if let centerX = centerX, centerX {
159
168
constraintManager. centerX = centerXAnchor. constraint ( equalTo: view. centerXAnchor)
169
+ constraintManager. centerX? . isActive = true
160
170
}
161
171
if let centerY = centerY, centerY {
162
172
constraintManager. centerY = centerYAnchor. constraint ( equalTo: view. centerYAnchor)
173
+ constraintManager. centerY? . isActive = true
174
+ }
175
+ let inActiveCount = [ constraintManager. width, constraintManager. height, constraintManager. top, constraintManager. leading,
176
+ constraintManager. bottom, constraintManager. trailing, constraintManager. centerX,
177
+ constraintManager. centerY] . filter ( { $0? . isActive == false } ) . count
178
+ if inActiveCount == 0 {
179
+ print ( " SnapLayout Error - No constraint was applied for view: \( String ( describing: view) ) " )
163
180
}
164
- [ constraintManager. top, constraintManager. leading, constraintManager. bottom, constraintManager. trailing,
165
- constraintManager. width, constraintManager. height, constraintManager. centerX, constraintManager. centerY] . forEach { $0? . isActive = true }
166
181
return constraintManager
167
182
}
168
183
@@ -207,7 +222,10 @@ public extension UIView {
207
222
/// - Returns: ConstraintManager holding all the values associated with constraints
208
223
@discardableResult
209
224
func snapWidth( to view: UIView ? = nil , multiplier: CGFloat = 1 ) -> ConstraintManager {
210
- guard let view = view ?? superview else { return ConstraintManager ( ) }
225
+ guard let view = view ?? superview else {
226
+ print ( " SnapLayout Error - width constraint not applied " )
227
+ return ConstraintManager ( )
228
+ }
211
229
translatesAutoresizingMaskIntoConstraints = false
212
230
let constraintManager = ConstraintManager ( )
213
231
constraintManager. width = widthAnchor. constraint ( equalTo: view. widthAnchor, multiplier: multiplier)
@@ -236,7 +254,10 @@ public extension UIView {
236
254
/// - Returns: ConstraintManager holding all the values associated with constraints
237
255
@discardableResult
238
256
func snapHeight( to view: UIView ? = nil , multiplier: CGFloat = 1 ) -> ConstraintManager {
239
- guard let view = view ?? superview else { return ConstraintManager ( ) }
257
+ guard let view = view ?? superview else {
258
+ print ( " SnapLayout Error - height constraint not applied " )
259
+ return ConstraintManager ( )
260
+ }
240
261
translatesAutoresizingMaskIntoConstraints = false
241
262
let constraintManager = ConstraintManager ( )
242
263
constraintManager. height = heightAnchor. constraint ( equalTo: view. heightAnchor, multiplier: multiplier)
0 commit comments