How iOS adds the Finish button in the lower left corner of the numeric keypad

Source: Internet
Author: User

Recently to add a custom completion button to the bottom left corner of the numeric keypad on the system pop-up, the implementation of the system's own keyboard-added custom button was studied. Summing up the general approach, the principle is this: When the page text box or other input source because the user clicks to become the first responder (Becomefirstresponder), the system keyboard will pop up. Each time the keyboard pops up or is closed, a related keyboard event, the notification message (notification), is sent to the system. So, we simply capture the relevant keyboard events when the keyboard pops up or down, and add or remove our custom buttons at the corresponding location on the window of the keyboard.

In this way, it is perfectly possible to add our own custom buttons when the keyboard pops up. However, if it is just such a simple process, the effect is: the keyboard popup and the custom button is added is not synchronized. Sometimes the keyboard has not been played by the custom button has been added to the attempt, and sometimes the keyboard pop-up after the custom button is added. So how do we make the keyboard pop-up and self-customization buttons can be added synchronously, to achieve this smooth over-pop effect, you also need to get the keyboard pop-up animation time and animation type, and then let the custom button to add animation and keyboard pop-up, the action of the synchronization.


1, first in the Viewwillappear method register to listen to the corresponding keyboard notification, and to write off the notification in the Viewwilldisappear method

-(void) Viewwillappear: (BOOL) Animated {        //Register keyboard display Notification    [[Nsnotificationcenter Defaultcenter] Addobserver:self Selector: @selector (handlekeyboarddidshow:) name:uikeyboardwillshownotification Object:nil];    Register for keyboard hide Notifications    [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (handlekeyboardwillhide:) Name:uikeyboardwillhidenotification Object:nil];        [Super viewwillappear:animated];}

-(void) Viewwilldisappear: (BOOL) animated{        //Logout keyboard display notification    [[Nsnotificationcenter Defaultcenter] Removeobserver : Self name:uikeyboardwillshownotification object:nil];    [[Nsnotificationcenter Defaultcenter] removeobserver:self name:uikeyboardwillhidenotification Object:nil];        [Super viewwilldisappear:animated];}


2. Handling keyboard Popup and collection events

Keyboard occurrence processing event-(void) Handlekeyboarddidshow: (nsnotification *) notification{//Nsnotification    The UserInfo dictionary contains information such as the location and size of the keyboard nsdictionary *userinfo = [notification UserInfo]; Uikeyboardanimationdurationuserinfokey corresponding keyboard pop-up animation time cgfloat animationduration = [[UserInfo objectforkey:    Uikeyboardanimationdurationuserinfokey] Floatvalue]; Uikeyboardanimationcurveuserinfokey the animation type that corresponds to the keyboard pop-up Nsinteger animationcurve = [[UserInfo objectforkey:    Uikeyboardanimationcurveuserinfokey] IntegerValue];        Digital color, numeric keypad add "Finish" button if (Doneinkeyboardbutton) {[UIView beginanimations:nil context:null]; [UIView setanimationduration:animationduration];//set animation time for adding buttons [UIView setanimationcurve: (Uiviewanimationcurve) animationcurve];//set the animation type of the Add button//Set the add location of the custom button (here to add the "Finish" button to the numeric keypad) Doneinkeyboardbutton.transform=cgaffin                Etransformtranslate (doneinkeyboardbutton.transform, 0,-53);    [UIView commitanimations]; }    }

Keyboard disappearance processing event-(void) Handlekeyboardwillhide: (nsnotification *) notification{    //Nsnotification The UserInfo dictionary contains information such as the location and size of the keyboard    nsdictionary *userinfo = [notification userInfo];    Uikeyboardanimationdurationuserinfokey the animation time of the corresponding keyboard is    cgfloat animationduration = [[UserInfo objectforkey: Uikeyboardanimationdurationuserinfokey] Floatvalue];    if (Doneinkeyboardbutton.superview)    {        [UIView animatewithduration:animationduration animations:^{            // Animated content, move the custom button back to the initial position            doneinkeyboardbutton.transform=cgaffinetransformidentity;        } completion:^ (BOOL finished {            ///After the end of the animation remove the custom button            [Doneinkeyboardbutton Removefromsuperview];        }];            }


3. Click the input box to initialize the custom button and eject the keyboard.

Click on the Input box-(ibaction) Editingdidbegin: (ID) sender{        //Initialize the "Finish" button of the numeric keypad    [self configdoneinkeyboardbutton];

Initialize, numeric keypad "Finish" button-(void) configdoneinkeyboardbutton{cgfloat screenheight = [UIScreen mainscreen].bounds.size.height; Initialize if (Doneinkeyboardbutton = = nil) {Doneinkeyboardbutton = [[UIButton buttonwithtype:uibuttontypecusto        M] retain];        [Doneinkeyboardbutton settitle:@ "completed" forstate:uicontrolstatenormal];                [Doneinkeyboardbutton Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];                Doneinkeyboardbutton.frame = CGRectMake (0, ScreenHeight, 106, 53);        doneinkeyboardbutton.adjustsimagewhenhighlighted = NO; [Doneinkeyboardbutton addtarget:self Action: @selector (finishaction) forcontrolevents:uicontroleventtouchupinside]    ;        }//The initial coordinate position of the "Finish" button must be newly set doneinkeyboardbutton.frame = CGRectMake (0, ScreenHeight, 106, 53); Due to iOS8, the window view where the keyboard is located has not yet been initialized, and the call will get the window view of the keyboard under the next Runloop [self performselector: @selector (Adddonebutton)    Withobject:nil afterdelay:0.0f]; }-(void) adddonebutton{//Get the WI the keyboard resides onNdow view uiwindow* Tempwindow = [[UIApplication sharedapplication] windows] objectatindex:1];    [Tempwindow Addsubview:doneinkeyboardbutton]; Note that this is added directly to the window}

Click the "Finish" button event to close the keyboard-(void) finishaction{[[[    uiapplication sharedapplication] Keywindow] endediting:yes];//turn off the keyboard}

According to this idea and the principle of implementation, you can add other custom controls such as input boxes on top of the keyboard, and can follow the keyboard pop-up and pick-up smoothing over.

How iOS adds the Finish button in the lower left corner of the numeric keypad

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.