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)