2

I am trying to get a keyboard size from userInfo using keyboard extension.

I add

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardDidShowNotification, object: nil) 

inside my KeyboardViewController.swift but keyboardWillShow never gets called.

3 Answers 3

8

First register for keyboard notifications

override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) let notificationCenter = NSNotificationCenter.defaultCenter() notificationCenter.addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) notificationCenter.addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil) } override func viewDidDisappear(animated: Bool) { super.viewDidDisappear(animated) let notificationCenter = NSNotificationCenter.defaultCenter() notificationCenter.removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) notificationCenter.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) } 

then

func keyboardWillShow(notification: NSNotification) { let userInfo = notification.userInfo! let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height } 

and you have your height value as a CGFloat

Sign up to request clarification or add additional context in comments.

Comments

0

In viewWillAppear, have you added self.subscribeToKeyboardNotifications() ? Just a guess, is your KeyboardViewController seperate from the ViewController in which the keyboard shows up? If so, have `NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardDidShowNotification, object: nil) in a func in the actual ViewController.

Comments

0

use following code for Swift 3.0

override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #selector(SignUpViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) notificationCenter.addObserver(self, selector: #selector(SignUpViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) let notificationCenter = NotificationCenter.default notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) } @objc func keyboardWillShow(notification: NSNotification) { let userInfo = notification.userInfo! let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height UIView.animate(withDuration: 0.3) { self.constraintBottomBtnNext.constant = keyboardHeight; } self.view.layoutIfNeeded(); print(keyboardHeight); } @objc func keyboardWillHide(notification: NSNotification) { UIView.animate(withDuration: 0.3) { self.constraintBottomBtnNext.constant = 0; } self.view.layoutIfNeeded(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.