iOS Development--swift Real-world & notification Keyboard Reality and Hide (Keyboard toolbar)

Source: Internet
Author: User

Before reading this article, it is recommended that the reader first understand the communication principle of notifying Nsnotifation

Bad description, I first:

is to click "Done" can hide the keyboard and their own, the keyboard came out when they also come out, yes, that's the effect, very common

1, set Keyboardheaderview and "Done" (here the Self.keyboardheaderview is set to the Self object)

1Self.keyboardHeaderView.frame = CGRect (x:0, Y:deviceframe.height+statusbarframe.height,width:deviceframe.width,height: -)2Self.keyboardHeaderView.backgroundColor = Uicolor (white:0, Alpha:0.6)3var Hiddenkeyboardlabel:uilabel = UILabel (Frame:cgrect (x:0Y:0, width:deviceframe.width-Ten, Height: -))4Hiddenkeyboardlabel.text ="Complete"5Hiddenkeyboardlabel.textcolor =Uicolor.bluecolor ()6Hiddenkeyboardlabel.textalignment = . Right7             8var Tap:uitapgesturerecognizer = UITapGestureRecognizer (Target:self,action:selector ("Hidekeyboard:"))9self.keyboardHeaderView.userInteractionEnabled =trueTen Self.keyboardHeaderView.addGestureRecognizer (TAP) One              A Self.keyboardHeaderView.addSubview (Hiddenkeyboardlabel) -Self.view.addSubview (Self.keyboardheaderview)

Click "Done" to invoke the method (Self.content here is a Uitextfield or Uitextview object)

 1//Hide keyboard234 }

2. Set the keyboard listener event:

1             Nsnotificationcenter.defaultcenter (). Addobserver (Self,selector:selector ("keyboardwillshow: "), Name:uikeyboardwillshownotification,object: nil)2             Nsnotificationcenter.defaultcenter (). Addobserver (Self,selector:selector ("keyboardwillhide: "), Name:uikeyboardwillhidenotification,object: nil)

Here is the event that is triggered when the keyboard is hidden, and I do what we want in this event (that is, the settings Keyboardheaderview follow the keyboard to hide and appear)

1   //Keyboard'll show2 func keyboardwillshow (sender:nsnotification) {3Let UserInfo =Sender.userinfo4Let Keyboardinfo: (anyobject!) =Userinfo.objectforkey (Uikeyboardframeenduserinfokey)5Let Keyboardrect:cgrect = Keyboardinfo.cgrectvalue () asCGRect6Let height = keyboardRect.size.height asFloat7         8Uiview.animatewithduration (0.3, animations: {9var y:float = deviceframe.height+statusbarframe.height-height-Self.keyboardHeaderView.frame.heightTenSelf.keyboardHeaderView.frame = CGRect (x:0, Y:y,width:deviceframe.width,height: -) One         }) A     } -      -     //keyboard would hide the func keyboardwillhide (sender:nsnotification) { -Uiview.animatewithduration (0.3, animations: { -Self.keyboardHeaderView.frame = CGRect (x:0, Y:deviceframe.height+statusbarframe.height,width:deviceframe.width,height: -) -         }) +}

After implementing this function, we are generally in the above reality a keyboard toolbar used to implement the emoji keyboard.

iOS Development--swift Real-world & notification Keyboard Reality and Hide (Keyboard toolbar)

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.