mirror of https://github.com/SnapKit/SnapKit
Reworked how constraints are stored against installed view
This commit is contained in:
parent
9fe13a042b
commit
0b687e37bd
|
@ -390,38 +390,38 @@ public class Constraint {
|
|||
installOnView!.addConstraints(layoutConstraints)
|
||||
|
||||
self.installedOnView = installOnView
|
||||
self.installedLayoutConstraints = NSHashTable.weakObjectsHashTable()
|
||||
for layoutConstraint in layoutConstraints {
|
||||
self.installedLayoutConstraints!.addObject(layoutConstraint)
|
||||
}
|
||||
|
||||
return layoutConstraints
|
||||
}
|
||||
|
||||
internal func uninstallFromView() {
|
||||
if let view = self.installedOnView {
|
||||
#if os(iOS)
|
||||
var installedConstraints = view.constraints()
|
||||
#else
|
||||
var installedConstraints = view.constraints
|
||||
#endif
|
||||
var constraintsToRemove: Array<LayoutConstraint> = []
|
||||
for installedConstraint in installedConstraints {
|
||||
if let layoutConstraint = installedConstraint as? LayoutConstraint {
|
||||
if layoutConstraint.constraint === self {
|
||||
constraintsToRemove.append(layoutConstraint)
|
||||
}
|
||||
}
|
||||
}
|
||||
if constraintsToRemove.count > 0 {
|
||||
view.removeConstraints(constraintsToRemove)
|
||||
// remove all installed layout constraints
|
||||
var layoutConstraintsToRemove = Array<LayoutConstraint>()
|
||||
if let installedLayoutConstraints = self.installedLayoutConstraints?.allObjects as? Array<LayoutConstraint> {
|
||||
layoutConstraintsToRemove += installedLayoutConstraints
|
||||
}
|
||||
|
||||
if layoutConstraintsToRemove.count > 0 {
|
||||
view.removeConstraints(layoutConstraintsToRemove)
|
||||
}
|
||||
|
||||
// clean up the snp_installedLayoutConstraints
|
||||
var layoutConstraints = view.snp_installedLayoutConstraints
|
||||
var layoutConstraintsToKeep = Array<LayoutConstraint>()
|
||||
for layoutConstraint in layoutConstraints {
|
||||
if !contains(constraintsToRemove, layoutConstraint) {
|
||||
if !contains(layoutConstraintsToRemove, layoutConstraint) {
|
||||
layoutConstraintsToKeep.append(layoutConstraint)
|
||||
}
|
||||
}
|
||||
view.snp_installedLayoutConstraints = layoutConstraintsToKeep
|
||||
}
|
||||
self.installedOnView = nil
|
||||
self.installedLayoutConstraints = nil
|
||||
}
|
||||
|
||||
// MARK: private
|
||||
|
@ -434,6 +434,7 @@ public class Constraint {
|
|||
private var priority: Float = 1000.0
|
||||
private var offset: Any?
|
||||
|
||||
private var installedLayoutConstraints: NSHashTable?
|
||||
private weak var installedOnView: View?
|
||||
|
||||
private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
|
||||
|
|
Loading…
Reference in New Issue