How do I implement keyboard following when using Snapkit layout?
In the previous article, our page used the Constraints constraint layout. If the page element is to use Snapkit this third party layout library to layout, also can realize keyboard follow up move effect.
The implementation is also to monitor the keyboard notification, and then change the constraints, so as to achieve the move animation effect.
The effect chart is as follows:
The code is as follows:
Import Uikit
Import Snapkit
Class Viewcontroller:uiviewcontroller {
Bottom tool bar under constraints
var bottomconstraint:constraint?
Bottom Toolbar view
var ToolBar = UIView ()
Send button
var sendbtn = UIButton (type:. System)
Text input Box
var TextField = Uitextfield ()
Override Func Viewdidload () {
Super.viewdidload ()
Add Bottom Toolbar view
Toolbar.backgroundcolor = Uicolor (red:0, green:0, blue:0, alpha:0.1)
Self.view.addSubview (ToolBar)
Set the bottom toolbar view constraint
toolbar.snp_makeconstraints {(make)-> Void in
Make.left.right.equalTo (Self.view)
Make.height.equalTo (50)
Self.bottomconstraint = Make.bottom.equalTo (self.view). Constraint
}
Add a button
Sendbtn.settitle ("Send", forstate:.) Normal)
Sendbtn.settitlecolor (Uicolor.whitecolor (), forstate:. Normal)
Sendbtn.backgroundcolor=uicolor.orangecolor ()
Sendbtn.addtarget (self,action: #selector (SendMessage (_:)), forControlEvents:. Touchupinside)
Toolbar.addsubview (SENDBTN)
Set button constraints
sendbtn.snp_makeconstraints {(make)-> Void in
Make.width.equalTo (60)
Make.height.equalTo (30)
Make.centerY.equalTo (ToolBar)
Make.right.equalTo (ToolBar). Offset (-10)
}
Add an input box
Textfield.borderstyle = Uitextborderstyle.roundedrect
Toolbar.addsubview (TextField)
Set Input Box constraints
textfield.snp_makeconstraints {(make)-> Void in
Make.left.equalTo (ToolBar). Offset (10)
Make.right.equalTo (sendbtn.snp_left). Offset (-10)
Make.height.equalTo (30)
Make.centerY.equalTo (ToolBar)
}
Listening for keyboard notifications
Nsnotificationcenter.defaultcenter (). Addobserver (Self,
Selector: #selector (Viewcontroller.keyboardwillchange (_:)),
Name:uikeyboardwillchangeframenotification, Object:nil)
}
Keyboard changes
Func Keyboardwillchange (notification:nsnotification) {
If let UserInfo = Notification.userinfo,
Value = Userinfo[uikeyboardframeenduserinfokey] as? Nsvalue,
Duration = Userinfo[uikeyboardanimationdurationuserinfokey] as? Double,
Curve = Userinfo[uikeyboardanimationcurveuserinfokey] as? UInt {
Let frame = value. Cgrectvalue ()
Let intersection = Cgrectintersection (frame, self.view.frame)
Self.view.setNeedsLayout ()
Change the constraint
Self.bottomconstraint? Updateoffset (-cgrectgetheight (intersection))
Uiview.animatewithduration (Duration, delay:0.0,
Options:uiviewanimationoptions (Rawvalue:curve),
Animations: {_ in
Self.view.layoutIfNeeded ()
}, Completion:nil)
}
}
Send button click
Func SendMessage (sender:anyobject) {
Turn off the keyboard
Textfield.resignfirstresponder ()
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}