1, constraint precedence
When we use Snapkit, we can also define the precedence of the constraint. In this way, when the constraint is conflicting, the precedence constraint overrides the lower precedence constraint. The specific priority can be placed at the end of the constraint chain.
(1) You can set the following several priority levels
Prioritylow (): Set low priority, priority is 250
Prioritymedium (): Set in priority, priority is 500 (this is also the default priority)
Priorityhigh (): Set high priority, priority to 750
Priority (): can set any priority, accept the parameter is 0-1000 of the number. For example: Priority (600)
(2) Use priority sample
Below we place a 100*100 Orange square in the center of the screen, giving it a default precedence constraint that has a long width size smaller than or equal to the size of the screen. At the same time, each time you click on the screen, it will be updated to enlarge its size. However, since the precedence of this constraint is low, all squares will not be enlarged until they are on the edge of the screen.
Import Uikit
Import Snapkit
Class Viewcontroller:uiviewcontroller {
lazy var box = UIView ()
var scacle = 1.0
Override Func Viewdidload () {
Super.viewdidload ()
Click Monitor
Let Tapsingle=uitapgesturerecognizer (target:self,action: #selector (Tapsingledid))
Tapsingle.numberoftapsrequired=1
Tapsingle.numberoftouchesrequired=1
Self.view.addGestureRecognizer (Tapsingle)
Box.backgroundcolor = Uicolor.orangecolor ()
Self.view.addSubview (Box)
box.snp_makeconstraints {(make)-> Void in
View Center
Make.center.equalTo (Self.view)
Initial width and height is 100 (Low priority)
Make.width.height.equalTo (* self.scacle). Prioritylow ();
Maximum size cannot exceed screen
Make.width.height.lessThanOrEqualTo (Self.view.snp_width)
Make.width.height.lessThanOrEqualTo (Self.view.snp_height)
}
}
Click on the screen
Func Tapsingledid () {
Self.scacle + 0.5
self.box.snp_updateconstraints{(make)-> Void in
Enlarge view (Lowest priority)
Make.width.height.equalTo (* self.scacle). Prioritylow ();
}
}
}
2, with animation effect
With Uiview.animatewithduration, we can animate when the constraint changes.
Or with the example above demo, not enough this time to click on the screen when the orange view magnification will have a transition, rather than the larger.
Import Uikit
Import Snapkit
Class Viewcontroller:uiviewcontroller {
lazy var box = UIView ()
var scacle = 1.0
Override Func Viewdidload () {
Super.viewdidload ()
Click Monitor
Let Tapsingle=uitapgesturerecognizer (target:self,action: #selector (Tapsingledid))
Tapsingle.numberoftapsrequired=1
Tapsingle.numberoftouchesrequired=1
Self.view.addGestureRecognizer (Tapsingle)
Box.backgroundcolor = Uicolor.orangecolor ()
Self.view.addSubview (Box)
box.snp_makeconstraints {(make)-> Void in
View Center
Make.center.equalTo (Self.view)
Initial width and height is 100 (Low priority)
Make.width.height.equalTo (* self.scacle). Prioritylow ();
Maximum size cannot exceed screen
Make.width.height.lessThanOrEqualTo (Self.view.snp_width)
Make.width.height.lessThanOrEqualTo (Self.view.snp_height)
}
}
View Constraint update
Override Func Updateviewconstraints () {
self.box.snp_updateconstraints{(make)-> Void in
Enlarge size (Low priority)
Make.width.height.equalTo (* self.scacle). Prioritylow ();
}
Super.updateviewconstraints ()
}
Click on the screen
Func Tapsingledid () {
Self.scacle + 0.5
Tell Self.view constraints need to be updated
Self.view.setNeedsUpdateConstraints ()
Animation
Uiview.animatewithduration (0.3) {
Self.view.layoutIfNeeded ()
}
}
}
Original: www.hangge.com