IOS keyboard height and keyboard response events

Source: Internet
Author: User

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 have marked the height of the keyboard.

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:

[[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.

#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 should 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:

[[NSNotificationCenter defaultCenter] 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.

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.