mirror of
https://github.com/MessageKit/MessageKit.git
synced 2026-02-06 19:03:19 +00:00
Switch calls to NSLayoutConstraint.(de)activate
AppleDocs say these are typically faster than directly adjusting isActive
This commit is contained in:
@@ -48,22 +48,19 @@ public class NSLayoutConstraintSet {
|
||||
self.width = width
|
||||
self.height = height
|
||||
}
|
||||
|
||||
private func forEach(_ body: (NSLayoutConstraint) -> Void) {
|
||||
let constraints = [top, bottom, left, right, centerX, centerY, width, height]
|
||||
for constraint in constraints {
|
||||
if let constraint = constraint {
|
||||
body(constraint)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// All of the currently configured constraints
|
||||
private var availableConstraints: [NSLayoutConstraint] {
|
||||
return [top, bottom, left, right, centerX, centerY, width, height]
|
||||
.flatMap {$0}
|
||||
}
|
||||
|
||||
/// Activates all of the non-nil constraints
|
||||
///
|
||||
/// - Returns: Self
|
||||
@discardableResult
|
||||
func activate() -> Self {
|
||||
forEach { $0.isActive = true }
|
||||
NSLayoutConstraint.activate(availableConstraints)
|
||||
return self
|
||||
}
|
||||
|
||||
@@ -72,7 +69,7 @@ public class NSLayoutConstraintSet {
|
||||
/// - Returns: Self
|
||||
@discardableResult
|
||||
func deactivate() -> Self {
|
||||
forEach { $0.isActive = false }
|
||||
NSLayoutConstraint.deactivate(availableConstraints)
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,10 +31,14 @@ extension UIView {
|
||||
return
|
||||
}
|
||||
translatesAutoresizingMaskIntoConstraints = false
|
||||
leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true
|
||||
rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true
|
||||
topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
|
||||
bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
|
||||
|
||||
let constraints: [NSLayoutConstraint] = [
|
||||
leftAnchor.constraint(equalTo: superview.leftAnchor),
|
||||
rightAnchor.constraint(equalTo: superview.rightAnchor),
|
||||
topAnchor.constraint(equalTo: superview.topAnchor),
|
||||
bottomAnchor.constraint(equalTo: superview.bottomAnchor),
|
||||
]
|
||||
NSLayoutConstraint.activate(constraints)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@@ -83,7 +87,7 @@ extension UIView {
|
||||
constraints.append(constraint)
|
||||
}
|
||||
|
||||
constraints.forEach { $0.isActive = true }
|
||||
NSLayoutConstraint.activate(constraints)
|
||||
return constraints
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user