Get height of iOS keyboard without displaying keyboard
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?
Swift
You can get the keyboard height by subscribing to the UIKeyboardWillShowNotification
notification. (Assuming you want to know what the height will be before it's shown).
Swift 4
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil
)
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
}
}
Swift 3
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil
)
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
}
}
Swift 2
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
let userInfo: NSDictionary = notification.userInfo!
let keyboardFrame: NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()
let keyboardHeight = keyboardRectangle.height
}
Get Keyboard frame without keyboardWillAppear userinfo notification, iOS
You can use a UITextField
and call becomeFirstResponder
and immediately resignFirstResponder
on it, so it will be shown and hidden without actually seeing the keyboard.
the action method of the button will look like the following:
@IBAction func showPickerView(sender: AnyObject)
{
let textField = UITextField()
view.addSubview(textField)
textField.becomeFirstResponder()
textField.resignFirstResponder()
textField.removeFromSuperview()
}
And you could listen to the notification and get the height.
for further information: Get height of iOS keyboard without UIKeyboardWillShowNotification
How to get keyboard height WITHOUT relying on notifications
You still have to use NSNotificationCenter, but you have to observe a different key. The key you're looking for is UIKeyboardDidChangeFrameNotification which according to the docs is posted immediately after a change in the keyboard’s frame.
Related Topics
How to Permanently Allow Usage of Camera on Trusted Websites with iOS - Safari
Open App from Sms with My Url Scheme as a Link
The Bundle's Info.Plist Does Not Contain a Cfbundleversion Key or Its Value Is Not a String
iOS Client Certificates and Mobile Device Management
Application Identifier Entitlement Value Has Changed
Instagram API Prevents Redirection to iOS App Upon Successful Login with 400 Bad Request Error
Flutter: Disable Swipe to Navigate Back in iOS and Android
How to Display Uiview Over Keyboard in iOS
How to Select a Picker View Item in an iOS UI Test in Xcode
Stuck on Using Mkpinannotationview() Within Swift and Mapkit
Detect When a Unicode Character Cannot Be Displayed Correctly
How to Customise Uislider Height
Get Height of iOS Keyboard Without Displaying Keyboard
How to Save Tableview Cell's Checkmark After Reload View Use Nsuserdefaults
Keep Bluetooth Sound When Initializing Avaudiosession
iOS Custom Annotation: a View Below the Annotation Pin
Alamofire 5: Value of Type 'Result<Data, Aferror>' Has No Member 'Value'