mirror of https://github.com/SnapKit/SnapKit
Added support for updateConstraints
This commit is contained in:
parent
5a968ae864
commit
9251b638af
|
@ -303,7 +303,7 @@ public class Constraint {
|
||||||
|
|
||||||
// MARK: internal
|
// MARK: internal
|
||||||
|
|
||||||
internal func install() -> Array<LayoutConstraint> {
|
internal func install(updateExisting: Bool = false) -> Array<LayoutConstraint> {
|
||||||
if self.installedOnView != nil {
|
if self.installedOnView != nil {
|
||||||
NSException(name: "Cannot Install Constraint", reason: "Already installed", userInfo: nil).raise()
|
NSException(name: "Cannot Install Constraint", reason: "Already installed", userInfo: nil).raise()
|
||||||
return []
|
return []
|
||||||
|
@ -367,6 +367,42 @@ public class Constraint {
|
||||||
layoutConstraints.append(layoutConstraint)
|
layoutConstraints.append(layoutConstraint)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// special logic for updating
|
||||||
|
if updateExisting {
|
||||||
|
// get existing constraints for this view
|
||||||
|
let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
|
||||||
|
|
||||||
|
// array that will contain only new layout constraints
|
||||||
|
var newLayoutConstraints = Array<LayoutConstraint>()
|
||||||
|
|
||||||
|
// begin looping
|
||||||
|
for layoutConstraint in layoutConstraints {
|
||||||
|
// layout constraint that should be updated
|
||||||
|
var updateLayoutConstraint: LayoutConstraint? = nil
|
||||||
|
|
||||||
|
// loop through existing and check for match
|
||||||
|
for existingLayoutConstraint in existingLayoutConstraints {
|
||||||
|
if existingLayoutConstraint == layoutConstraint {
|
||||||
|
updateLayoutConstraint = existingLayoutConstraint
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we have existing one lets just update the constant
|
||||||
|
if updateLayoutConstraint != nil {
|
||||||
|
updateLayoutConstraint!.constant = layoutConstraint.constant
|
||||||
|
}
|
||||||
|
// otherwise add this layout constraint to new list
|
||||||
|
else {
|
||||||
|
newLayoutConstraints.append(layoutConstraint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set constraints to only new ones
|
||||||
|
layoutConstraints = newLayoutConstraints
|
||||||
|
}
|
||||||
|
|
||||||
|
// add constarints
|
||||||
installOnView!.addConstraints(layoutConstraints)
|
installOnView!.addConstraints(layoutConstraints)
|
||||||
|
|
||||||
self.installedOnView = installOnView
|
self.installedOnView = installOnView
|
||||||
|
|
|
@ -100,6 +100,23 @@ public class ConstraintMaker {
|
||||||
view.snp_installedLayoutConstraints = layoutConstraints
|
view.snp_installedLayoutConstraints = layoutConstraints
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal class func updateConstraints(view: View, block: (make: ConstraintMaker) -> ()) {
|
||||||
|
#if os(iOS)
|
||||||
|
view.setTranslatesAutoresizingMaskIntoConstraints(false)
|
||||||
|
#else
|
||||||
|
view.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
#endif
|
||||||
|
let maker = ConstraintMaker(view: view)
|
||||||
|
block(make: maker)
|
||||||
|
|
||||||
|
var layoutConstraints = view.snp_installedLayoutConstraints
|
||||||
|
for constraint in maker.constraints {
|
||||||
|
layoutConstraints += constraint.install(updateExisting: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
view.snp_installedLayoutConstraints = layoutConstraints
|
||||||
|
}
|
||||||
|
|
||||||
internal class func removeConstraints(view: View) {
|
internal class func removeConstraints(view: View) {
|
||||||
for existingLayoutConstraint in view.snp_installedLayoutConstraints {
|
for existingLayoutConstraint in view.snp_installedLayoutConstraints {
|
||||||
existingLayoutConstraint.constraint?.uninstall()
|
existingLayoutConstraint.constraint?.uninstall()
|
||||||
|
|
|
@ -32,4 +32,29 @@ import AppKit
|
||||||
*/
|
*/
|
||||||
public class LayoutConstraint: NSLayoutConstraint {
|
public class LayoutConstraint: NSLayoutConstraint {
|
||||||
internal var constraint: Constraint?
|
internal var constraint: Constraint?
|
||||||
|
}
|
||||||
|
|
||||||
|
public func ==(left: LayoutConstraint, right: LayoutConstraint) -> Bool {
|
||||||
|
if left.firstItem !== right.firstItem {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if left.secondItem !== right.secondItem {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if left.firstAttribute != right.firstAttribute {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if left.secondAttribute != right.secondAttribute {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if left.relation != right.relation {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if left.priority != right.priority {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if left.multiplier != right.multiplier {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
Loading…
Reference in New Issue