In the previous article I introduced how to use the Snapkit Snp_makeconstraints method to set various constraints. But sometimes our pages are not always fixed, which requires modifying existing constraints. This article describes how to update, remove, and replace existing constraints.
1, delete the constraint
To update or remove an existing constraint, we need to assign the result of the constraint to a local variable or a class property, and then manipulate the reference to that constraint.
For example: At the beginning we added a 40 pixel constraint to the orange Square to the top of the screen, then click the button and use the uninstall () method to remove the constraint.
Import Uikit
Import Snapkit
Class Viewcontroller:uiviewcontroller {
lazy var box = UIView ()
Save a reference to a constraint
var topconstraint:constraint?
Override Func Viewdidload () {
Super.viewdidload ()
Box.backgroundcolor = Uicolor.orangecolor ()
Self.view.addSubview (Box)
box.snp_makeconstraints {(make)-> Void in
Make.width.height.equalTo (150)
Make.centerX.equalTo (Self.view)
Self.topconstraint = Make.top.equalTo (self.view). Offset (+). Constraint
}
}
button click
@IBAction func Btntouch (sender:anyobject) {
Remove constraint
Self.topconstraint? Uninstall ()
}
}
2, updating constraints by reference to constraints
Or, for example, with a constraint above 40 pixels above the screen, click the button to modify the distance to 60 pixels by invoking the Updateoffset () method of the constraint reference.
Import Uikit
Import Snapkit
Class Viewcontroller:uiviewcontroller {
lazy var box = UIView ()
Save a reference to a constraint
var topconstraint:constraint?
Override Func Viewdidload () {
Super.viewdidload ()
Box.backgroundcolor = Uicolor.orangecolor ()
Self.view.addSubview (Box)
box.snp_makeconstraints {(make)-> Void in
Make.width.height.equalTo (150)
Make.centerX.equalTo (Self.view)
Self.topconstraint = Make.top.equalTo (self.view). Offset (+). Constraint
}
}
button click
@IBAction func Btntouch (sender:anyobject) {
Update Modify Constraint
Self.topconstraint? Updateoffset (60)
}
}
3, using the snp_updateconstraints update constraint
We can also use the Snp_updateconstraints method instead of the snp_makeconstraints to constrain the update, which is usually placed in the Uiviewcontroller updateviewconstraints () method, or in the UIView updateconstraints () method, which is invoked automatically when the view constraint needs to be updated.
For example, we use the Snp_updateconstraints () method to set the width constraint of the orange view to be as wide as the screen, so that the view is automatically updated to fill the screen regardless of how the device rotates.
Import Uikit
Import Snapkit
Class Viewcontroller:uiviewcontroller {
lazy var box = UIView ()
Override Func Viewdidload () {
Super.viewdidload ()
Box.backgroundcolor = Uicolor.orangecolor ()
Self.view.addSubview (Box)
box.snp_makeconstraints {(make)-> Void in
Make.height.equalTo (150)
Make.centerX.equalTo (Self.view)
}
}
View Constraint update
Override Func Updateviewconstraints () {
self.box.snp_updateconstraints{(make)-> Void in
Width of view and screen
Make.width.equalTo (Self.view)
}
Super.updateviewconstraints ()
}
}
4, using snp_remakeconstraints to redo the constraint
Snp_remakeconstraints is similar to the snp_makeconstraints usage, but the snp_remakeconstraints first clears all previous constraints that were set by Snapkit.
The following example: When initialized, the orange view constraint is 150 wide and horizontally centered. Click the button to redo the constraint, the width and height of 100, and no longer center.
Import Uikit
Import Snapkit
Class Viewcontroller:uiviewcontroller {
lazy var box = UIView ()
Override Func Viewdidload () {
Super.viewdidload ()
Box.backgroundcolor = Uicolor.orangecolor ()
Self.view.addSubview (Box)
box.snp_makeconstraints {(make)-> Void in
Make.width.height.equalTo (150)
Make.centerX.equalTo (Self.view)
}
}
button click
@IBAction func Btntouch (sender:anyobject) {
Redo Constraint
box.snp_remakeconstraints {(make)-> Void in
Make.width.height.equalTo (100)
}
}
}