IOS usage notification (nsnotificationcenter) Gets the height of the keyboard, as well as considerations for modifying the interface when displaying and hiding the keyboard

Source: Internet
Author: User

We will encounter this situation in development: The keyboard requires an interface to adjust to avoid the keyboard to obscure the input box.

But when implemented you will find that the height of the keyboard on different phones is different. Here's a list:

//get the height of the keyboard    /*     iphone 6: Chinese 2014-12-31 11:16:23.643 demo[686:41289] keyboard height is 258 2014-12-31 11:16:23.644 demo[686:41289] Key Disk width is 375 english 2014-12-31 11:55:21.417 demo[1102:58972] keyboard height is 216 2014-12-31 11:55:21.417 demo[1102:58972] keyboard width  is 375 iphone 6 Plus: English: 2014-12-31 11:31:14.669 demo[928:50593] keyboard height is 226 2014-12-31 11:31:14.669 DEMO[928:50593] Keyboard width is 414 Chinese: 2015-01-07 09:22:49.438 demo[622:14908] keyboard height is 271 2015-01-07 09:22:49.439 Dem o[622:14908] Keyboard width is 414 iPhone 7 2016-11-13 17:44:48.315 logindemo[2431:120937] keyboard height is 258 2016-11-13 17: 44:48.316 logindemo[2431:120937] Keyboard width is 375 iPhone 7 Plus 2016-11-13 17:43:20.683 logindemo[2322:119257] keyboard height Yes 271 2016-11-13 17:43:20.683 logindemo[2322:119257] Keyboard width is 414 iphone 5:2014-12-31 11:19:36.452 demo[ 755:43,233] Keyboard height is 216 2014-12-31 11:19:36.452 demo[755:43233] keyboard width is Apple ipad air:2014-12-31 11:28:32.17 8 demo[851:48085] Keyboard height is 2014-12-31 11:28:32.178 demo[851:48085] keyboard width is 768 ipad2:2014-12-31 11:33:57.258 demo[1014:53043]      Keyboard height is 2014-12-31 11:33:57.258 demo[1014:53043] keyboard width is 768*/

We see different phone device keyboard height is different, and the English and Chinese keyboard height is not the same.

Let's talk about using notifications to get the height of the keyboard:

// increase monitoring to receive messages when the keyboard appears or changes [[Nsnotificationcenter defaultcenter] addobserver:self                                      selector: @selector (keyboardwillshow:)                                      Name: Uikeyboardwillshownotification                                      object// increase listener, when key exits when the message is received [[ Nsnotificationcenter Defaultcenter] addobserver:self                                      selector: @selector (keyboardwillhide:)                                      Name: Uikeyboardwillhidenotification                                      object: nil];

Display the code for the keyboard:

- (void) Keyboardwillshow: (Nsnotification *) anotification{//get the height of the keyboardNsdictionary *userinfo =[Anotification UserInfo]; Nsvalue*avalue =[UserInfo Objectforkey:uikeyboardframeenduserinfokey]; CGRect Keyboardrect=[Avalue Cgrectvalue]; intHeight =KeyboardRect.size.height; intwidth =KeyboardRect.size.width; NSLog (@"keyboard height is%d", height); NSLog (@"keyboard width is%d", width);}

Hide the height of the keyboard:

// when the keyboard is hidden -(void) Keyboardwillbehidden: (nsnotification*) anotification{     / /dosomething}

Of course, after the registration notice (nsnotificationcenter), remember the last cancellation notice:

-(void) viewwilldisappear: (BOOL) animated{    [[Nsnotificationcenter Defaultcenter] Removeobserver: Self];}

notice ?? : Uikeyboardwillshownotification is also generated when switching keyboards , so it is possible that the method is called multiple times. It is best not to write code that is related to the number of calls in keyboardwillshow . When the keyboard is first generated, it will be called multiple times if it is not the default English keyboard.

Like what:

#pragma mark-  ****************** open keyboard ********-(void) KEYBOARDWILLSHOWOFFEEDBACKVC: ( Nsnotification *) notify {        // This is incorrect: This method is called when you switch the English keyboard.  //    cgrect frame = self.view.frame; //     frame.origin.y = self.view.frame.origin.y-64; //     self.view.frame = frame; = CGRectMake (0,-iphone_width, iphone_height);}          

IOS usage notification (nsnotificationcenter) Gets the height of the keyboard, as well as considerations for modifying the interface when displaying and hiding 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.