Mario Hu
Mario Hu

Reputation: 77

How can I get the keyboard's height?

I must get the keyboard's height during the UI create! So I can't use the method that monitor the keyboard's height when it appear. I just get the final static height, not dynamic~~how can I do it

Upvotes: 0

Views: 884

Answers (3)

Nicola Giancecchi
Nicola Giancecchi

Reputation: 3065

I can say to you that the keyboard height is 216px in portrait and 162px in landscape, but using a fixed keyboard height is a bad choice. Since iOS 8, the user can choose between different third-party keyboard and enable or disable predictive texting, which can make the keyboard frame taller.

iOS provides handy notification types which can be observed into your view controller. These notifications are:

  • NSNotification.Name.UIKeyboardWillShow: posted immediately prior to the display of the keyboard.
  • NSNotification.Name.UIKeyboardDidShow: posted immediately after the display of the keyboard.
  • NSNotification.Name.UIKeyboardWillHide: posted immediately prior to the dismissal of the keyboard.
  • NSNotification.Name.UIKeyboardDidHide: posted immediately after the dismissal of the keyboard.
  • NSNotification.Name.UIKeyboardWillChangeFrame: posted immediately prior to a change in the keyboard’s frame.
  • NSNotification.Name.UIKeyboardDidChangeFrame: posted immediately after a change in the keyboard’s frame.

You can register to these notifications by adding an observer into the viewDidLoad or viewDidAppear method of your view controller:

NotificationCenter.default.addObserver(self, selector: #selector(YourViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

Also, remember to remove it into your deinit / viewDidDisappear method: NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)

Create the method associated to your observer (in this case, keyboardWillShow(notification:):

func keyboardWillShow(notification : Notification) {
    let info:NSDictionary = notification.userInfo! as NSDictionary
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    self.keyboardHeight = Double(keyboardSize.height)
}

Reference: https://developer.apple.com/reference/foundation/nsnotification.name

Upvotes: 2

MacUserT
MacUserT

Reputation: 1953

I did it in a method that is called after a keyboard notification. It looks like this:

    func keyboardDidShow(notification: NSNotification) {

    let keyboardInfo = notification.userInfo
    let keyboardRect = keyboardInfo?[UIKeyboardFrameBeginUserInfoKey] as! CGRect
    let keyboardSize = keyboardRect.size
    var overviewRectangle = self.view.frame

    overviewRectangle.size.height += keyboardSize.height
    self.overviewScrollView.contentSize = overviewRectangle.size
    self.overviewScrollView.setContentOffset(CGPoint.init(x: 0, y: (overviewRectangle.origin.y + keyboardSize.height)), animated: true)
}

func keyBoardDidDisappear(notification: NSNotification) {
    let overviewRectangle = self.view.frame

    self.overviewScrollView.contentSize = overviewRectangle.size
    self.overviewScrollView.setContentOffset(CGPoint.init(x: 0, y: overviewRectangle.origin.y), animated: true)

}

Upvotes: -1

Arun K
Arun K

Reputation: 840

If you need height for keyboard handling then my friend you are doing it the wrong way, just use this cool library and write this is you app delegate

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  IQKeyboardManager.sharedManager().enable = true

  return true
}

This library will handle all your hassle for keyboard handling

Upvotes: 0

Related Questions