What is the height of iPad's onscreen keyboard?

前端 未结 12 2102
傲寒
傲寒 2020-12-08 03:45

I\'m looking for two numbers here: the height in portrait and the height in landscape. Don\'t answer in cm or inches, but rather in pixels.

12条回答
  •  抹茶落季
    2020-12-08 04:21

    I've voted down a couple of answers on this page, as they are dangerously misleading.

    Certainly, with iOS 8, you cannot use a hard-coded keyboard height.

    Here's my app, running on an iPhone, to give my reason why.

    Grab your ruler, and tell me, what is the height of the onscreen keyboard..?

    enter image description here

    Ahh... you can't do it, can you ?

    The height can vary, if the user turns on the auto-type bar at the top of the onscreen keyboard (which, by the way, triggers off a second "keyboardWillShow" event).

    The same is true for the onscreen keyboard on the iPad.

    Bottom line: sorry folks, you must measure the keyboard height in a keyboardWillShow event.

    You will hit problems otherwise.

    - (void)keyboardWillShow:(NSNotification *)notification {
    
        NSDictionary *info = [notification userInfo];
        NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardFrame = [kbFrame CGRectValue];
        keyboardFrame = [self.view convertRect:keyboardFrame fromView:nil];
    
        CGFloat heightOfKeyboard = keyboardFrame.size.height;
    
        NSLog(@"The keyboard is now %d pixels high.", (int)heightOfKeyboard);
    
    }
    

    Btw, don't forget that call to convertRect in the code above.

    Without it, I found that, on an iPad running in landscape with iOS 8, sometimes the keyboard would be reported as having a height of 1024 pixels..

    Just one more thing...

    Today, I was working on a custom control I'd written, which appears at the bottom of the iPhone/iPad screen, and realised I hadn't taken the onscreen keyboard height into consideration.

    I didn't want to add keyboardWillShow/Hide notifications, as this little control was generic, able to be called from any of my screens.

    My solution to find the height of the onscreen keyboard, if it was currently visible, was to use the excellent visibleKeyboardHeight function from the free SVProgressHUD control.

    That control also needed the height of the onscreen keyboard, so its notification message UIViews would appear vertically-centered.

    It worked a treat... so if you do need a quick'n'dirty method of grabbing the keyboard height, grab a copy of this code, and look in the SVProgressHUD.m file.

    Download SVProgressHUD from GitHub

提交回复
热议问题