Skip to content
This repository was archived by the owner on Sep 15, 2019. It is now read-only.

Commit 5424286

Browse files
author
Satinder Singh
committed
[BUG] Fixed SnapLayout not setting size constraint
- SnapLayout would not set constraint for width nor height constraint when superview not specified - Fixed by not relying on superview for these constraint settings - Added more documentation - Added debugger print messages
1 parent 0974c73 commit 5424286

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

SnapLayout.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'SnapLayout'
11-
s.version = '1.0.4'
11+
s.version = '1.0.5'
1212
s.summary = 'Concise API for iOS Auto Layout'
1313

1414
s.description = <<-DESC

SnapLayout/Classes/SnapLayout.swift

+32-11
Original file line numberDiff line numberDiff line change
@@ -129,40 +129,55 @@ public extension UIView {
129129
/// - trailing: Constant to apply from trailingAnchor (if nil, not applied)
130130
/// - width: Constant to apply from widthAnchor (if nil, not applied)
131131
/// - 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
132135
/// - Returns: ConstraintManager holding all the values associated with constraints
133136
@discardableResult
134137
func snap(to view: UIView? = nil, top: CGFloat? = nil, leading: CGFloat? = nil, bottom: CGFloat? = nil,
135138
trailing: CGFloat? = nil, width: CGFloat? = nil, height: CGFloat? = nil, centerX: Bool? = nil,
136139
centerY: Bool? = nil) -> ConstraintManager {
137-
guard let view = view ?? superview else { return ConstraintManager() }
138140
translatesAutoresizingMaskIntoConstraints = false
139141
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 }
140151
if let top = top {
141152
constraintManager.top = topAnchor.constraint(equalTo: view.topAnchor, constant: top)
153+
constraintManager.top?.isActive = true
142154
}
143155
if let leading = leading {
144156
constraintManager.leading = leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: leading)
157+
constraintManager.leading?.isActive = true
145158
}
146159
if let bottom = bottom {
147160
constraintManager.bottom = bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: bottom)
161+
constraintManager.bottom?.isActive = true
148162
}
149163
if let trailing = trailing {
150164
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
157166
}
158167
if let centerX = centerX, centerX {
159168
constraintManager.centerX = centerXAnchor.constraint(equalTo: view.centerXAnchor)
169+
constraintManager.centerX?.isActive = true
160170
}
161171
if let centerY = centerY, centerY {
162172
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))")
163180
}
164-
[constraintManager.top, constraintManager.leading, constraintManager.bottom, constraintManager.trailing,
165-
constraintManager.width, constraintManager.height, constraintManager.centerX, constraintManager.centerY].forEach { $0?.isActive = true }
166181
return constraintManager
167182
}
168183

@@ -207,7 +222,10 @@ public extension UIView {
207222
/// - Returns: ConstraintManager holding all the values associated with constraints
208223
@discardableResult
209224
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+
}
211229
translatesAutoresizingMaskIntoConstraints = false
212230
let constraintManager = ConstraintManager()
213231
constraintManager.width = widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: multiplier)
@@ -236,7 +254,10 @@ public extension UIView {
236254
/// - Returns: ConstraintManager holding all the values associated with constraints
237255
@discardableResult
238256
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+
}
240261
translatesAutoresizingMaskIntoConstraints = false
241262
let constraintManager = ConstraintManager()
242263
constraintManager.height = heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: multiplier)

0 commit comments

Comments
 (0)