Add dynamic Hide button on iOS virtual keyboard

Source: Internet
Author: User

To add a button to the keyboard, in real time according to the keyboard different height Change button position, do not do input when the click button can hide the keyboard, this way in a lot of software are reflected, and then check the Internet on the detection of keyboard height of some relevant knowledge, the following is a demo, code has a lot to optimize the place, Only for the need of reference;

First look at the effect:

The first is that we have registered two notices in Viewdidloada (), [Nsnotificationcenterdefaultcenter], detect keyboard dynamics, one is the keyboard will be ejected, the other is the keyboard will exit when the keyboard information

-(void) viewdidload

{

NSLog (@ "%@", Nsstringfromselector (_cmd));

[Super Viewdidload];

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (handlekeyboarddidshow:) Name: Uikeyboardwillshownotification Object:nil];

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (handlekeyboardwillhide:) Name: Uikeyboardwillhidenotification Object:nil];

}

Detect keyboard messages A six kinds, almost all of which can explain function by literal meaning

Uikeyboardwillshownotification notification will be released when the keyboard is displayed

Uikeyboarddidshownotification display keyboard immediately after notification is released

Uikeyboardwillhidenotification to revoke the keyboard before issuing notification

Uikeyboarddidhidenotification the keyboard after notification is released

Uikeyboardwillchangeframenotification notifies the keyboard of a rapidly changing frame before publishing.

Uikeyboarddidchangeframenotification notice is released immediately after the frame changes in the keyboard.

NSLog (@ "%@", Nsstringfromselector (_cmd)); I added it specifically, it can print out the function called by the current program in the console, I added this sentence to each function below, and when I do different things, I print out the name of the called function, It is more applicable when debugging the program;

The method that implements the notification response after registering a message notification

-(void) Handlekeyboarddidshow: (nsnotification *) notification

{

NSLog (@ "%@", Nsstringfromselector (_cmd));

Nsdictionary *info = [notification UserInfo];

CGRect Keyboardframe;

[[Info Objectforkey:uikeyboardframeenduserinfokey] getvalue:&keyboardframe];

Cgsize kbsize = [info objectforkey:uikeyboardframeenduserinfokey]cgrectvalue].size;

CGFloat distancetomove = kbsize.height;

NSLog (@ "----> Dynamic keyboard Height:%f", distancetomove);

if (Exitbutton = = nil) {

Exitbutton = [UIButton buttonwithtype:uibuttontyperoundedrect];

CGRect exitbtframe = CGRectMake (self.view.frame.size.width-40, Self.view.frame.size.height-distancetomove, 40.0f, 30.0f);

Exitbutton.frame = Exitbtframe;

[Exitbutton setimage:[uiimage imagenamed:@ "Donedown.png"] forstate:uicontrolstatenormal];

[Self.view Addsubview:exitbutton];

}

Exitbutton.hidden=no;

[Self adjustpanelswithkeybordheight:distancetomove];

[Exitbutton addtarget:self Action: @selector (Cancelbackkeyboard:) Forcontrolevents:uicontroleventtouchdown];

}

What is worth exploring in this function method is the information about the keyboard, because every time the keyboard pops up, it's also animated, and his coordinates are in the UserInfo dictionary, and now I use

NSLog (@ "-->info:%@", info), print out the Info object, this information can be in different storage types, value of the time to pay attention to the value of the way, this is just a mention, hope that there will be time to do the discussion,

In this section of code, 5 lines are commented on, because the button is intended to be removed from the view when the keyboard is rolled out, or the release button is released, but it all causes the application to crash, and then there is no release or removal of the operation.

-(void) Handlekeyboardwillhide: (nsnotification *) notification

{

NSLog (@ "%@", Nsstringfromselector (_cmd));

if (Exitbutton.hidden==no) {

Exitbutton.hidden = YES;

}

if (Exitbutton.superview)

// {

[Exitbutton Removefromsuperview];

[Exitbutton release];

// }

}

-(void) Adjustpanelswithkeybordheight: (float) Height

{

NSLog (@ "%@", Nsstringfromselector (_cmd));

if (Exitbutton) {

CGRect exitbtframe = CGRectMake (self.view.frame.size.width-40, self.view.frame.size.height-height-30, 40.0f, 30.0f);

Exitbutton.frame = Exitbtframe;

[Self.view Addsubview:exitbutton];

}

uiwindow* Tempwindow = [[[UIApplication sharedapplication] windows] objectatindex:1];

if (Exitbutton.superview = = nil)

// {

[Tempwindow Addsubview:exitbutton];

Note that this is added directly to the window.

// }

}

-(void) Cancelbackkeyboard: (ID) sender

{

NSLog (@ "%@", Nsstringfromselector (_cmd));

[TextField Resignfirstresponder];

}

-(void) viewdidunload

{

[Self settextfield:nil];

Exitbutton=nil;

[Super Viewdidunload];

Release any retained subviews of the main view.

}

-(void) Dealloc {

[TextField release];

[Exitbutton release];

[[Nsnotificationcenter Defaultcenter] removeobserver:self];//removal of registered notifications

[Super Dealloc];

}

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.