Get height of iOS keyboard without displaying keyboard

ios get keyboard height objective-c
swift get keyboard height
get keyboard height swift4
keyboardwillchangeframe
uikeyboardframeenduserinfokey wrong height
uikeyboardwillchangeframenotification

I'm trying to get the height of the iOS keyboard. I've gone through and used the method involving subscribing to a notification such as detailed here: https://gist.github.com/philipmcdermott/5183731

- (void)viewDidAppear:(BOOL) animated {
    [super viewDidAppear:animated];
    // Register notification when the keyboard will be show
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillShow:)
        name:UIKeyboardWillShowNotification
        object:nil];

    // Register notification when the keyboard will be hide
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillHide:)
        name:UIKeyboardWillHideNotification
        object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyboardBounds;

    [[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBounds];

    // Do something with keyboard height
}

- (void)keyboardWillHide:(NSNotification *)notification {
    CGRect keyboardBounds;

    [[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBounds];

    // Do something with keyboard height
}

This works fine for when the user actually displays the keyboard.

My problem: I have another view, let's call it micView, that may be presented before the keyboard appears. The user may choose to use the microphone before typing. I would like the micView to be the same height as the keyboard, which is why I need the keyboard's height, but I need it before the keyboard was forced to appear. Thus the UIKeyboardWillShowNotification is not reached before I need to read the value of the height.

My question is: how do I get the height of the keyboard through Notifications, or some other method without ever having the keyboard appear.

I considered explicitly forcing the keyboard to appear in viewDidLoad, so that I can set an instance variable to that value, then hiding it and getting rid of the animations for both things. But is that really the only way to do that?


A quick solution that you could use, is the same one used when you want to cache the keyboard (the first time you show it, you get a slight delay...). The library is here. The interesting bits:

[[[[UIApplication sharedApplication] windows] lastObject] addSubview:field];
[field becomeFirstResponder];
[field resignFirstResponder];
[field removeFromSuperview];

So basically is showing it and then hiding it. You could listen for notifications and just get the height without actually seeing it. Bonus: you get to cache it. :)

How to get height of Keyboard?, In Swift: You can get the keyboard height by subscribing to the UIKeyboardWillShowNotification notification. (Assuming you want to know what the height will be  Fixing the second issue is easy. You need to observe when the keyboard appears and get its height. Once you have the height you can increase the scroll view's content insets by that height. This adds padding to the bottom of the scroll view, so everything that was covered by the keyboard is pushed up.


This Swift class provides a turn-key solution that manages all the necessary notifications and initializations, letting you simply call a class method and have returned the keyboard size or height.

Calling from Swift:

let keyboardHeight = KeyboardService.keyboardHeight()
let keyboardSize = KeyboardService.keyboardSize()

Calling from Objective-C:

 CGFloat keyboardHeight = [KeyboardService keyboardHeight];
 CGRect keyboardSize = [KeyboardService keyboardSize];

If wanting to use this for initial view layout, call this from the viewWillAppear method of a class where you want the keyboard height or size before the keyboard appears. It should not be called in viewDidLoad, as a correct value relies on your views having been laid out. You can then set an autolayout constraint constant with the value returned from the KeyboardService, or use the value in other ways. For instance, you might want to obtain the keyboard height in prepareForSegue to assist in setting a value associated with the contents of a containerView being populated via an embed segue.

Note re safe area, keyboard height, and iPhone X: The value for keyboard height returns the full height of the keyboard, which on the iPhone X extends to the edge of the screen itself, not just to the safe area inset. Therefore, if setting an auto layout constraint value with the returned value, you should attach that constraint to the superview bottom edge, not to the safe area.

Note re hardware keyboard in Simulator: When a hardware keyboard is attached, this code will provide the on-screen height of that hardware keyboard, that is, no height. This state does need to be accounted for, of course, as this simulates what will occur if you have a hardware keyboard attached to an actual device. Therefore, your layout that is expecting a keyboard height needs to respond appropriately to a keyboard height of zero.

KeyboardService class: As usual, if calling from Objective-C, you simply need to import the app's Swift bridging header MyApp-Swift.h in your Objective-C class.

import UIKit

class KeyboardService: NSObject {
    static var serviceSingleton = KeyboardService()
    var measuredSize: CGRect = CGRect.zero

    @objc class func keyboardHeight() -> CGFloat {
        let keyboardSize = KeyboardService.keyboardSize()
        return keyboardSize.size.height
    }

    @objc class func keyboardSize() -> CGRect {
        return serviceSingleton.measuredSize
    }

    private func observeKeyboardNotifications() {
        let center = NotificationCenter.default
        center.addObserver(self, selector: #selector(self.keyboardChange), name: .UIKeyboardDidShow, object: nil)
    }

    private func observeKeyboard() {
        let field = UITextField()
        UIApplication.shared.windows.first?.addSubview(field)
        field.becomeFirstResponder()
        field.resignFirstResponder()
        field.removeFromSuperview()
    }

    @objc private func keyboardChange(_ notification: Notification) {
        guard measuredSize == CGRect.zero, let info = notification.userInfo,
            let value = info[UIKeyboardFrameEndUserInfoKey] as? NSValue
            else { return }

        measuredSize = value.cgRectValue
    }

    override init() {
        super.init()
        observeKeyboardNotifications()
        observeKeyboard()
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }    
}

Head nod: The observeKeyboard method here based on the original approach outlined by Peres in the Objective-C answer to this question.

Getting the keyboard height, (I believe there was some confusion because of the safe area inset on the iPhone X.) To illustrate that displaying the view was possible, I set up a  Some third party virtual keyboards will offer lower/slimmer height keys. If you are looking for a third party keyboard with variable height keys as well as more easy access to most common keys and symbols and with the addition of text “I-beam” navigation keys, I would recommend PadKeys for your iPad.


Get keyboard height with Swift, Getting the keyboard height with Swift is easy, and can be used when we 1 print("Keyboard show notification") // 2 if let userInfo = notification. When the keyboard is displayed, all you have to do is reset the content area of the scroll view and scroll the desired text object into position. Thus, in response to a UIKeyboardDidShowNotification, your handler method would do the following: Get the size of the keyboard. Adjust the bottom content inset of your scroll view by the keyboard height.


Swift 5.1 solution:

import UIKit

// MARK: - Properties & Lifecycle
class Keyboard {
    static private let shared = Keyboard()
    static private(set) var size = CGRect.zero
    static var isVisible: Bool = false

    static var height: CGFloat {
        return size.height
    }

    init() {
        observeKeyboardNotifications()
        triggerKeyboardNotification()
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

// MARK: - NotificationCenter
extension Keyboard {
    private func observeKeyboardNotifications() {
        NotificationCenter.default.addObserver(self, selector: #selector(self.handleKeyboardShow), name: UIResponder.keyboardDidShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.handleKeyboardHide), name: UIResponder.keyboardDidHideNotification, object: nil)
    }

    private func triggerKeyboardNotification() {
        let field = UITextField()
        UIApplication.shared.windows.first?.addSubview(field)
        field.becomeFirstResponder()
        field.resignFirstResponder()
        field.removeFromSuperview()
    }

    @objc private func handleKeyboardShow(_ notification: Notification) {
        Keyboard.isVisible = true

        if let info = notification.userInfo, let value = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
            Keyboard.size = value.cgRectValue
        }
    }

    @objc private func handleKeyboardHide(_ notification: Notification) {
        Keyboard.isVisible = false
    }
}

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  These problems are faced due to software updating to iOS 12 or 11.3, charging or screen replacement. The main issues are that the iPhone iPad keyboard missing or they do not appear or showing up on the screen. There are several more situations of the keyboard not working that include keyboard being frozen, lag in typing and no popping of keyboard.


Height of keyboard in swift - Programmatically, So that developer's can meet the app requirements where they have to show button above keyboard, when user is typing on text field or other input field. iOS  Set your Display Zoom view to zoomed, and change the text size to whatever size you want. This will allow your keyboard to turn bigger and the font to stay the same size. These are a few third party apps: Minuum. Thick Buttons


🤽 🧑 ‍🤝‍🧑 🤳 Get iOS keyboard height without displaying a , The quick solution you could use is the same one that is used when you want to cache the keyboard (the first time you display it, you get a slight delay ). When the system shows or hides the keyboard, it posts several keyboard notifications. These notifications contain information about the keyboard, including its size, which you can use for calculations that involve repositioning or resizing views. Registering for these notifications is the only way to get some types of information about the


iOS: How to get a keyboard height programmatically, There is a simple way, maybe it saves time for someone. For resolve this task you should register UIKeyboardWillChangeFrameNotification,  Top of Page. Ribbon keyboard shortcuts. The ribbon groups related options on tabs. For example, on the Home tab, the Number group includes the Number Format option. Press the Alt key to display the ribbon shortcuts, called Key Tips, as letters in small images next to the tabs and options as shown in the image below.