iOS handling of the keyboard

Source: Internet
Author: User

Method 1. Using <UITEXTFEILDDELEGATE&GT, use the Uitextfield example to set its delegate to self, and click the return button to hide the keyboard. The implementation functions are as follows:-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
{
[TextField Resignfirstresponder]; return YES;
} Method 2. Click on other blank areas of the interface to hide
Since Uiviewcontroller is inherited from Uiresponder, it is possible to overwrite-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event; This starts the touch method to cancel the first responder, the code is as follows:

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

{

[_tfpassword Resignfirstresponder];

[_tfusername Resignfirstresponder];

}

Both of these methods are used when learning iOS.3. It is better to use the Notificationcenter method is better, can obtain the keyboard height and other details
#import "ViewController.h" @interface Viewcontroller () @property (weak, nonatomic) Iboutlet Uitextfield *tftest;@  Property (weak, nonatomic) Iboutlet Uitextfield *tftest2; @end @implementation viewcontroller-(void) Viewdidload {[Super Viewdidload];    }-(void) Viewdidappear: (BOOL) animated{[Super viewdiddisappear:animated]; Remove keyboard listener message [[Nsnotificationcenter defaultcenter]removeobserver:self Name:UIKeyboardWillShowNotificationobject:nil    ];    [[Nsnotificationcenter defaultcenter]removeobserver:self Name:UIKeyboardWillHideNotificationobject:nil]; Registering a keyboard listener message [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (keyshow:) name:uikeyboardwills    Hownotification Object:nil]; [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (keyhide:) Name: Uikeyboardwillhidenotification Object:nil];}    Popup Keyboard Message response-(void) Keyshow: (nsnotification *) no{NSLog (@ "Keyshow");    Nsdictionary *dic = [no valueforkey:@ "UserInfo"]; CGFloat heightkeyboard = [[dicvalueforkey:@ "Uikeyboardboundsuserinfokey"]cgrectvalue].size.height;    UIView *firstresponderview = [self getfirstresponderview];    CGFloat distancetobottom = Self.view.frame.size.height-cgrectgetmaxy (firstresponderview.frame);        Animation if (Distancetobottom < Heightkeyboard) {CGRect frame = self.view.frame;        FRAME.ORIGIN.Y =-(Heightkeyboard-distancetobottom);        [UIView animatewithduration:0.3f animations:^{[Self.view setframe:frame];    } completion:^ (BOOL finished) {}];    }}//off Keyboard message response-(void) Keyhide: (nsnotification *) no{NSLog (@ "Keyhide");    CGRect frame = self.view.frame;    FRAME.ORIGIN.Y = 0;    Animation [UIView animatewithduration:0.3f animations:^{[Self.view setframe:frame]; } completion:^ (BOOL finished) {}];} Click the background area to automatically hide the keyboard-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{UIView *    Firstresponderview = [self getfirstresponderview]; [Firstresponderview Resignfirstresponder];}//gets the control in which the current focus is located-(UIView *) getfirstresponderview{UIWindow *keywindow = [[UIApplication Sharedapplication]keywindow];    UIView *firstresponder = [Keywindow performselector: @selector (FirstResponder)]; return firstresponder;} -(void) didreceivememorywarning {[Super didreceivememorywarning];}  @end

  

iOS handling of the keyboard

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.