mirror of https://github.com/SnapKit/SnapKit
Reworked how constraints are stored against installed view
This commit is contained in:
parent
9fe13a042b
commit
0b687e37bd
|
@ -268,13 +268,13 @@ public class Constraint {
|
||||||
self.offset = amount
|
self.offset = amount
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: install / uninstall
|
// MARK: install / uninstall
|
||||||
|
|
||||||
public func install() -> Array<LayoutConstraint> {
|
public func install() -> Array<LayoutConstraint> {
|
||||||
return self.installOnView(updateExisting: false)
|
return self.installOnView(updateExisting: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func uninstall() {
|
public func uninstall() {
|
||||||
self.uninstallFromView()
|
self.uninstallFromView()
|
||||||
}
|
}
|
||||||
|
@ -297,14 +297,14 @@ public class Constraint {
|
||||||
} else {
|
} else {
|
||||||
installOnView = self.fromItem.view?.superview
|
installOnView = self.fromItem.view?.superview
|
||||||
if installOnView == nil {
|
if installOnView == nil {
|
||||||
if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
|
if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
|
||||||
installOnView = self.fromItem.view
|
installOnView = self.fromItem.view
|
||||||
}
|
}
|
||||||
|
|
||||||
if installedOnView == nil {
|
if installedOnView == nil {
|
||||||
NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
|
NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,40 +388,40 @@ public class Constraint {
|
||||||
|
|
||||||
// add constraints
|
// add constraints
|
||||||
installOnView!.addConstraints(layoutConstraints)
|
installOnView!.addConstraints(layoutConstraints)
|
||||||
|
|
||||||
self.installedOnView = installOnView
|
self.installedOnView = installOnView
|
||||||
|
self.installedLayoutConstraints = NSHashTable.weakObjectsHashTable()
|
||||||
|
for layoutConstraint in layoutConstraints {
|
||||||
|
self.installedLayoutConstraints!.addObject(layoutConstraint)
|
||||||
|
}
|
||||||
|
|
||||||
return layoutConstraints
|
return layoutConstraints
|
||||||
}
|
}
|
||||||
|
|
||||||
internal func uninstallFromView() {
|
internal func uninstallFromView() {
|
||||||
if let view = self.installedOnView {
|
if let view = self.installedOnView {
|
||||||
#if os(iOS)
|
// remove all installed layout constraints
|
||||||
var installedConstraints = view.constraints()
|
var layoutConstraintsToRemove = Array<LayoutConstraint>()
|
||||||
#else
|
if let installedLayoutConstraints = self.installedLayoutConstraints?.allObjects as? Array<LayoutConstraint> {
|
||||||
var installedConstraints = view.constraints
|
layoutConstraintsToRemove += installedLayoutConstraints
|
||||||
#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)
|
if layoutConstraintsToRemove.count > 0 {
|
||||||
|
view.removeConstraints(layoutConstraintsToRemove)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clean up the snp_installedLayoutConstraints
|
||||||
var layoutConstraints = view.snp_installedLayoutConstraints
|
var layoutConstraints = view.snp_installedLayoutConstraints
|
||||||
var layoutConstraintsToKeep = Array<LayoutConstraint>()
|
var layoutConstraintsToKeep = Array<LayoutConstraint>()
|
||||||
for layoutConstraint in layoutConstraints {
|
for layoutConstraint in layoutConstraints {
|
||||||
if !contains(constraintsToRemove, layoutConstraint) {
|
if !contains(layoutConstraintsToRemove, layoutConstraint) {
|
||||||
layoutConstraintsToKeep.append(layoutConstraint)
|
layoutConstraintsToKeep.append(layoutConstraint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
view.snp_installedLayoutConstraints = layoutConstraintsToKeep
|
view.snp_installedLayoutConstraints = layoutConstraintsToKeep
|
||||||
}
|
}
|
||||||
self.installedOnView = nil
|
self.installedOnView = nil
|
||||||
|
self.installedLayoutConstraints = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: private
|
// MARK: private
|
||||||
|
@ -434,6 +434,7 @@ public class Constraint {
|
||||||
private var priority: Float = 1000.0
|
private var priority: Float = 1000.0
|
||||||
private var offset: Any?
|
private var offset: Any?
|
||||||
|
|
||||||
|
private var installedLayoutConstraints: NSHashTable?
|
||||||
private weak var installedOnView: View?
|
private weak var installedOnView: View?
|
||||||
|
|
||||||
private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
|
private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
|
||||||
|
|
Loading…
Reference in New Issue