Background:
Before ios5, the keyboard height on the iPhone was fixed to 216213px high, and the selection box of Chinese characters was suspended, therefore, many applications use this height to mark the keyboard height (this is also true for MI chat ).
However, in ios5, the keyboard layout has changed, especially when the Chinese character selection box is fixed above the keyboard, in this way, the interface view that originally closely fits the keyboard is overwritten by the Chinese character selection box. On the one hand, it affects the appearance of the interface, and on the other hand, if the covered part is a text input box, the user will not be able to see the input content. Therefore, this problem must be solved.
Solution:
In fact, it is wrong to use the fixed value 216213px to mark the keyboard height at the beginning. In systems later than ios3.2, Apple provided the keyboard-used API and demo program-"keyboardaccessory ".
The correct method for handling Keyboard Events is as follows: (including obtaining the keyboard height and the time when the keyboard pops up and disappears)
1) in the View Controller to use the keyboard, receive notifications of Keyboard Events:
Objective-C code
- [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardwillshow :) name: uikeyboardwillshownotification object: Nil];
- [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardwillhide :) name: uikeyboardwillhidenotification object: Nil];
- // Keyboard height change notification, added by ios5.0
- # Ifdef _ iphone_5_0
- Float version = [[[uidevice currentdevice] systemversion] floatvalue];
- If (version> = 5.0 ){
- [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (keyboardwillshow :) name: uikeyboardwillchangeframenotification object: Nil];
- }
- # Endif
2) then add the code for handling Keyboard Events:
Obtain the height of the Current keyboard and the animation time, and then perform corresponding operations on The View.
Objective-C code
- # Pragma mark-
- # Pragma mark responding to Keyboard Events
- -(Void) keyboardwillshow :( nsnotification *) Notification {
- /*
- Reduce the size of the text view so that it's not obscured by the keyboard.
- Animate the resize so that it's in sync with the appearance of the keyboard.
- */
- Nsdictionary * userinfo = [Notification userinfo];
- // Get the origin of the keyboard when it's displayed.
- Nsvalue * avalue = [userinfo objectforkey: uikeyboardframeenduserinfokey];
- // Get the top of the keyboard as the Y coordinate of its origin in self's view's coordinate system. the bottom of the Text View's frame shoshould align with the top of the keyboard's final position.
- Cgrect keyboardrect = [avalue cgrectvalue];
- // Get the duration of the animation.
- Nsvalue * animationdurationvalue = [userinfo objectforkey: uikeyboardanimationdurationuserinfokey];
- Nstimeinterval animationduration;
- [Animationdurationvalue getvalue: & animationduration];
- // Animate the resize of the Text View's frame in sync with the keyboard's appearance.
- [Self moveinputbarwithkeyboardheight: keyboardrect. Size. Height withduration: animationduration];
- }
- -(Void) keyboardwillhide :( nsnotification *) Notification {
- Nsdictionary * userinfo = [Notification userinfo];
- /*
- Restore the size of the text view (fill self's view ).
- Animate the resize so that it's in sync with the disappearance of the keyboard.
- */
- Nsvalue * animationdurationvalue = [userinfo objectforkey: uikeyboardanimationdurationuserinfokey];
- Nstimeinterval animationduration;
- [Animationdurationvalue getvalue: & animationduration];
- [Self moveinputbarwithkeyboardheight: 0.0 withduration: animationduration];
- }
3) when the View Controller is eliminated, the keyboard Event Notification is removed:
Objective-C code
- [[Nsicationcenter center defacenter center] removeobserver: Self];
PS:
Ios5 hide function sharing-"Dictionary" function (English dictionary ):
Select an English word in any input box. In this case, select "copy" and "delete "... and so on, there is a right arrow, click the right arrow, the "Definition" option will appear, click this "Definition" button will pop up the English word explanation.