Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - knowing the keyboard size when it is already shown

Tags:

iphone

OK, I know I can read the keyboard size using the notification UIKeyboardWillShowNotification and this

keybSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

but this will just be available when the keyboard shows.

but the problem is this: I have a window that has to adjust itself to a new position when the keyboard is visible and when iPhone rotates. When I rotate the iPhone both delegates method willRotateToInterfaceOrientation and didRotateFromInterfaceOrientation run and handle the rotation. Inside these methods I need to know the current keyboard height, so I can position the view properly. The problem is that the method fired by the UIKeyboardWillShowNotification notification runs after the rotation was handled by the delegate methods.

The order the methods run is:

  1. willRotateToInterfaceOrientation
  2. didRotateFromInterfaceOrientation
  3. UIKeyboardWillShowNotification method

In other words, the keyboard height is just read at the end, what means both methods 1 and 2 will use the old keyboard height.

My question is: is there a way to read the keyboard height of a visible keyboard directly instead of relying in methods fired by notifications?

thanks

like image 339
Duck Avatar asked Mar 28 '11 18:03

Duck


People also ask

How do I get my keyboard back to normal on iPhone?

Go to Settings > Accessibility > Keyboards, tap Full Keyboard Access, then turn on Full Keyboard Access.

How do I resize my iPhone keyboard?

Tap and hold on the emoji or globe icon on the keyboard. You will see three keyboard icons . Tap the left icon to shrink the keyboard to the left or the right icon to shrink the keyboard to the right. Tap the middle icon to return to the full-sized keyboard.

What is keyboard height in iOS?

The top of the keyboard needs to be about 2 inches from the bottom of the *device* as it is held. Prior to the iPhone X, this is easy because all devices used the exact same bezel insets, so it's 216 pts from the bottom of the screen.

Can I adjust iPhone keyboard?

Go to Settings > General > Keyboard > Keyboards. Tap a language at the top of the screen, then select an alternative layout from the list.


1 Answers

Variant 1

You can use this code snippet to explore the view hierarchy:

- (void)printSubviews:(UIView *)aView {
    NSLog(@"%@ - %@", [aView class], NSStringFromCGRect([aView frame]));
    for (UIView *view in [aView subviews]) {
        [self printSubviews:view];
    }
}


- (void)printAllViews {
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        [self printSubviews:window];
    }
}

You'll see that it's pretty easy to figure out the keyboard size from the values you get.

Variant 2

Alternatively, you can import UIKeyboard.h (you can find it on the internet). It includes a method to get the default keyboard size for various interface orientations. However, that would make use of private APIs, so you might want to be careful with it.

like image 179
ryyst Avatar answered Sep 17 '22 13:09

ryyst