Keyboard handling during iOS input; ios Input Keyboard handling

Source: Internet
Author: User

Keyboard handling during iOS input; ios Input Keyboard handling

During iOS development, you sometimes need to handle keyboard pop-up and retrieval.

And how to process the view during keyboard pop-up retrieval

The most formal method is to use notifications.

 

Step 1:
Add monitoring when entering the view: (viewDidLoad or something)

 

// Listen to the keyboard notification [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (keyboardWillShow :) name: jsonobject: nil]; [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (keyboardWillHide :) name: UIKeyboardWillHideNotification object: nil];

 


Step 2:
Move the view during keyboard action:

 

- (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];    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];        CGFloat keyboardTop = keyboardRect.origin.y;    CGRect newTextViewFrame = self.view.bounds;    newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;        // 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.    [UIView beginAnimations:nil context:NULL];    [UIView setAnimationDuration:animationDuration];        self.reportTableView.frame = newTextViewFrame;    [UIView commitAnimations];}- (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];        [UIView beginAnimations:nil context:NULL];    [UIView setAnimationDuration:animationDuration];        self.reportTableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height  - 55 - 64);        [UIView commitAnimations];}

 

 



Step 3:
Logout notification when exiting View
ViewDidUnload:

     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

 

Step 4:
Dealloc:

-(void)dealloc{    [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];}

 


These codes are from apple sample code KeyboardAccessory.

 

Modify the details by yourself, such as the reportTableView and CGRectMake (0, 0, [UIScreen mainScreen]. bounds. size. width, [UIScreen mainScreen]. bounds. size. height-55-64)

 

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.