Get the keyboard height of iOS devices and the keyboard height of iOS devices
Recently, I made a custom keyboard. First, I want to know the height of the various keyboards on iOS devices. Next I will explain how to get the height of the keyboard.
The main function is to use the keyboard pop-up notification.
1. First, create a project.
2. Add the keyboard pop-up and hidden notifications to the project's-(void) viewDidload; function. The Code is as follows:
1 // Add a listener. When the keyboard displays or changes, the message is received. 2 [[nsicationicationcenter defacenter center] addObserver: self 3 selector: @ selector (keyboardWillShow :) 4 name: UIKeyboardWillShowNotification 5 object: nil]; 6 7 // Add a listener. When the key exits, the Message 8 [[nsicationicationcenter defacenter center] addObserver: self 9 selector: @ selector (keyboardWillHide :) 10 name: UIKeyboardWillHideNotification11 object: nil];
3. When a notification is received, write two functions to respond to the notification-(void) keyboardWillShow;-(void) keyboardWillHide;
Some keyboard attributes can be obtained in these two functions. The Code is as follows:
1-(void) keyboardWillShow :( NSNotification *) aNotification 2 {3 // get the keyboard height 4/* 5 iphone 6: 6 Chinese characters 7 11:16:23. 643 Demo [686: 41289] the keyboard height is 258 8 11:16:23. 644 Demo [686: 41289] the keyboard width is 375 9 English 10 11:55:21. 417 Demo [1102: 58972] the keyboard height is 21611 11:55:21. 417 Demo [1102: 58972] the keyboard width is 37512 13 iphone 6 plus: 14 English: 15 11:31:14. 669 Demo [928: 50593] the keyboard height is 22616 11:31:14. 669 Demo [928: 50593] the keyboard width is 41417 Chinese: 18 09:22:49. 438 Demo [622: 14908] the keyboard height is 27119 09:22:49. 439 Demo [622: 14908] the keyboard width is 41420 21 iphone 5: 22 11:19:36. 452 Demo [755: 43233] the keyboard height is 21623 11:19:36. 452 Demo [755: 43233] the keyboard width is 32024 25 ipad Air: 26 11:28:32. 178 Demo [851: 48085] the keyboard height is 26427 11:28:32. 178 Demo [851: 48085] the keyboard width is 76828 29 ipad2: 30 11:33:57. 258 Demo [11:33:57 53043] the keyboard height is 26431. 258 Demo [53043] the keyboard width is 76832 */33 NSDictionary * userInfo = [aNotification userInfo]; 34 NSValue * aValue = [userInfo objectForKey: UIKeyboardFrameEndUserInfoKey]; 35 CGRect keyboardRect = [aValue CGRectValue]; 36 int height = keyboardRect. size. height; 37 int width = keyboardRect. size. width; 38 NSLog (@ "the keyboard height is % d", height); 39 NSLog (@ "the keyboard width is % d", width ); 40} 41 42 // call 43-(void) keyboardWillHide (NSNotification *) aNotification44 {45} when the key exits}