Swift uses the Snapkit layout to enable the bottom input box to float after the keyboard appears

Source: Internet
Author: User

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 ()
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.