-1

I've been working on getting the height of the keyboard in swift 4.2. I've been using the Notification Center to call a function when the keyboard shows which accepts a notification as a param, which I think is pretty standard? Here I want to calculate the height. However the notification object is missing data and I can't calculate the height.

NSConcreteNotification 0x16e43a710 {
    name = UIKeyboardDidShowNotification; userInfo = {
       UIKeyboardAnimationCurveUserInfoKey = 7;
       UIKeyboardAnimationDurationUserInfoKey = "0.25";
       UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {0, 0}}";
       UIKeyboardCenterBeginUserInfoKey = "NSPoint: {0, 0}";
       UIKeyboardCenterEndUserInfoKey = "NSPoint: {0, 0}";
       UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 0}, {0, 0}}";
       UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 0}, {0, 0}}";
       UIKeyboardIsLocalUserInfoKey = 1;
    }
}

I call a commonInit function from my init method which sets up the methods to call:

private func commonInit() {
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: UIResponder.keyboardDidShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

}

KeyboardWillShow Function

@objc private func keyboardWillShow(notification:NSNotification) {

    let userInfo = notification.userInfo! as NSDictionary
    let keyboardFrame = userInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height

    print(keyboardHeight)
}

Is there something missing to get the notification data?

Keith Darragh
  • 439
  • 1
  • 6
  • 13
  • Are you trying in simulator or actual device? If you are trying in simulator than press Command+K to open the keyboard. Is the keyboard gets appear? – Vatsal K Nov 23 '18 at 15:13
  • I've tried it in both simulator and device. The keyboard appears on screen but I don't get enough notification data to calculate the height. The notification object is the same each time – Keith Darragh Nov 23 '18 at 15:16
  • you want to calculate the height of keyboard only? – Wings Nov 23 '18 at 15:17
  • try this link: https://stackoverflow.com/questions/31774006/how-to-get-height-of-keyboard – Vatsal K Nov 23 '18 at 15:18
  • Possible duplicate of [How to get height of Keyboard?](https://stackoverflow.com/questions/31774006/how-to-get-height-of-keyboard) – Vatsal K Nov 23 '18 at 15:19
  • Yes, I want to calculate the height only. I have UITextField elements on screen and want to move them up when the keyboard appears – Keith Darragh Nov 23 '18 at 15:22
  • Okay I am giving you my code just try it your code shows some error when i tried `Type 'UIResponder' has no member 'keyboardDidShowNotification'` – Wings Nov 23 '18 at 15:24
  • UIResponser has the member as of swift 4.2 I believe – Keith Darragh Nov 23 '18 at 15:35
  • I don't understand why it is giving me the error try DungeonDev answer – Wings Nov 23 '18 at 15:36
  • Could you share on what iOS version are you testing. I just tested your code and is working correctly on simulator with iOS 12.1. – gulyashki Nov 23 '18 at 15:46
  • Hey I'm currently trying to deploy on 12.1 but I've tried to deploy on 11.0 and both give the same results. It is also getting built using the new build system – Keith Darragh Nov 23 '18 at 16:08
  • The question makes no sense and provides insufficient information. The notification logged in the first code is not either of the notifications you register for, and you don't show any code that logs the notification. No one knows when you call `commonInit`. The whole thing is not reproducible. We all know how to register for keyboard show/hide, and there are lots and lots of working examples; you, on the other hand, are clearly insisting on doing something quite different and then not even showing us what it is. – matt Nov 23 '18 at 19:42
  • This keyboard interaction is part of a framework. When you start the framework it kicks off the main view controller in the framework. It starts a new UIView which contains the keyboard interaction. I can place the keyboard interacts in either the UIView or UIViewController and still get the same notification object shown. – Keith Darragh Nov 26 '18 at 12:07

1 Answers1

-1

That's what I always use:

func addObservers() {
        NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification,
                                               object: nil,
                                               queue: nil) { [weak self] (notification) in
                                                self?.keyboardWillShow(notification: notification as NSNotification)
        }

        NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification,
                                               object: nil,
                                               queue: nil) {  [weak self] (notification) in
                                                self?.keyboardWillHide(notification: notification as NSNotification)
        }

    }


func keyboardWillShow(notification: NSNotification) {
        if let userInfo = notification.userInfo {
            let keyboardHeight = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size.height
     }
     //... do your stuff...
 }
DungeonDev
  • 1,176
  • 1
  • 12
  • 16
  • Notification does not contain data to get the height. For some reason notification is missing data which allows the height to be calculated – Keith Darragh Nov 23 '18 at 15:40
  • How is this answer helpful? OP already showed that he's using `keyboardWillShowNotification` and that `keyboardFrameEndUserInfoKey` contains zero rect. – mag_zbc Nov 23 '18 at 15:45
  • @KeithDarragh: when do you call the commonInit() func? Could it be a problem in the VC lifecycle? I usually call in in the viewWillAppear function – DungeonDev Nov 23 '18 at 16:03
  • It's not getting called in a UIViewController - it's getting called in a UIView. So it gets called in its init function after super.init(frame: CGRect.zero). Does it matter where it's called for notifications of this type? – Keith Darragh Nov 23 '18 at 16:11
  • MMmm... even in a UIView it works, I've tried the code. Could you please show the whole code? – DungeonDev Nov 23 '18 at 22:44