Tag Archives: Keyboard

How to determine the height of the keyboard in iOS

Keyboards in iOS can have various sizes. Not only can a keyboard be presented in portrait or landscape, on various devices with a plethora of screen sizes, and users may have QuickType enabled or disabled. Too many variables for hard coding.

Lucky for us, we can check all kinds of properties about a keyboard by dissecting the dictionary that is passed with the notifications mentioned in my previous post. If you recall, we can check when a keyboard appears or disappears using an observer. Let’s take a look at that notification when the keyboard is presented, and we can extract the height like this:

- (CGFloat)grabKeyboardHeight:(NSNotification *)notification {
    
    // grab keyboard size
    NSDictionary *userInfo = notification.userInfo;
    NSValue *keyFrame = [userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = keyFrame.CGRectValue;
    CGFloat height = keyboardFrame.size.height;
    
    return height;
}

The keyboard’s width can be determined in much the same way. The dictionary holds many other interesting goodies, for example how long it takes iOS to animate it onto the screen:

- (NSTimeInterval)grabAnimationDuration:(NSNotification *)notification {
    
    // find out how long the keyboard animation lasts
    NSDictionary *userInfo = notification.userInfo;
    NSTimeInterval duration = [[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    
    return duration;
}

Apple has the full list if interesting objects for us to play with:





How to detect when the keyboard appears and disappears in iOS

To detect when the keyboard from a UITextField is being brought up (and goes away again), the UITextField Delegate Protocol won’t help us. Instead we need to listen to two notifications, namely UIKeyBoardWillShow and UIKeyboardWillHide.

We could setup the observers in viewDidLoad, like this for example:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // setup keyboard observers
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardCameUp:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWentAway:) name:UIKeyboardWillHideNotification object:nil];
}

These observers will call a method in our class (using @selector). Mine are called keyboardCameUp and keyboardWentAway:

- (void)keyboardCameUp:(NSNotification *)notification {
    
    NSLog(@"Keyboard came up!");
}

- (void)keyboardWentAway:(NSNotification *)notification {
    
    NSLog(@"Keyboard went away!");
}

The notification can be used to detect some properties of the keyboard, for example its height and width.





How to dismiss the iOS Keyboard when the done button is pressed

It has puzzled many generations how to get rid of the keyboard when the user is done entering text into an iOS text field. There doesn’t seem to be an obvious action we can attach to make this happen.

iOS Simulator Screen shot 31 Dec 2013 05.45.27

That’s because rather than looking at the keyboard itself, we need to look at what actually brought it up in the first place: the UITextField. Even though the text field can trigger an action, we don’t want to touch that for dismissing the keyboard. All we need to do is to implement the following method from its delegate protocol:

This content is for members only.