Keyboard occlusion control (textfield/textview...), textfieldtextview

Source: Internet
Author: User

Keyboard occlusion control (textfield/textview...), textfieldtextview

The conventional notification method is used.

// Solve the problem of keyboard occlusion
// Select didShow because the keyboard height is required.
// WillHide is selected because the view frame reset takes precedence over the absence of the keyboard. Otherwise, the view frame is inconsistent.
[[Nsicationcenter center defaultCenter] addObserver: self selector: @ selector (handler :) name: jsonobject: nil]; [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (keyboardWasHide :) name: UIKeyboardWillHideNotification object: nil];

Trigger Method

-(Void) keyboardWasShown :( NSNotification *) notification {// obtain the keyboard height NSValue * value = notification. userInfo [UIKeyboardFrameBeginUserInfoKey]; CGFloat keyBoradHeight = [value CGRectValue]. size. height; NSNumber * animationTime = notification. userInfo [UIKeyboardAnimationDurationUserInfoKey]; double time = [animationTime doubleValue]; // obtain the float a = self distance from the blocked control to the bottom of the controller. view4.frame. origin. y + self. view4.frame. size. height; float B = self. view. frame. size. height-a; if (B <keyBoradHeight) {// animation effect [UIView animateWithDuration: time animations: ^ {// the keyboard is blocked by CGRect viewCGrect = self. view. frame; // The view should be moved up so it is-viewCGrect. origin. y = viewCGrect. origin. y-(keyBoradHeight-B); [self. view setFrame: viewCGrect];} completion: nil];}-(void) keyboardWasHide :( NSNotification *) notification {NSValue * value = notification. userInfo [UIKeyboardFrameBeginUserInfoKey]; CGFloat keyBoradHeight = [value CGRectValue]. size. height; float a = self. view4.frame. origin. y + self. view4.frame. size. height; float B = self. view. frame. size. height-a; CGRect viewCGrect = self. view. frame; viewCGrect. origin. y = viewCGrect. origin. y + (keyBoradHeight-B); [self. view setFrame: viewCGrect];}

Last remove notification

-(void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    [[NSNotificationCenter defaultCenter] removeObserver:self                                                    name:UIKeyboardWillHideNotification                                                  object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self                                                    name:UIKeyboardDidShowNotification                                                  object:nil];}

 

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.