0

import UIKit

class KeyboardViewController: UIInputViewController {

 var heightKeyboard : CGFloat?

@IBOutlet var nextKeyboardButton: UIButton!

override func updateViewConstraints() {
    super.updateViewConstraints()

    // Add custom view sizing constraints here

}

override func viewDidLoad() {
    super.viewDidLoad()

    // Perform custom UI setup here
    self.nextKeyboardButton = UIButton(type: .system)

    self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), for: [])
    self.nextKeyboardButton.sizeToFit()
    self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false

    self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)

    self.view.addSubview(self.nextKeyboardButton)

    self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
    self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: UIResponder.keyboardDidShowNotification, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillAppear(true)
    NotificationCenter.default.removeObserver(self)
}

@objc func keyboardWillAppear(notification: NSNotification) {
    //Do something here
    print("keyboardWillAppear()")
    print("keyboardShown")
    if let infoKey  = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey],let rawFrame = (infoKey as AnyObject).cgRectValue {
       let keyboardFrame = view.convert(rawFrame, from: nil)
       self.heightKeyboard = keyboardFrame.size.height
 print(self.heightKeyboard)

   }
}

@objc func keyboardWillDisappear() {
     print("keyboardWillDisappear()")
}


override func viewWillLayoutSubviews() {
    self.nextKeyboardButton.isHidden = !self.needsInputModeSwitchKey
    super.viewWillLayoutSubviews()
}

override func textWillChange(_ textInput: UITextInput?) {
    // The app is about to change the document's contents. Perform any preparation here.
}

override func textDidChange(_ textInput: UITextInput?) {
    // The app has just changed the document's contents, the document context has been updated.

    var textColor: UIColor
    let proxy = self.textDocumentProxy
    if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
        textColor = UIColor.white
    } else {
        textColor = UIColor.black
    }
    self.nextKeyboardButton.setTitleColor(textColor, for: [])
}

} I am creating Keyboard Extension (swift) But unable to get height of keyboard . I am using storyboard for keyboard creation. func keyboardWillAppear(),func keyboardWillDisappear() never getting called.

So unable to get keyboard size based on different sizes and orientation

Amit Verma
  • 427
  • 4
  • 6
  • 1
    "func keyboardShown(notification: NSNotification) never getting called" And why would you expect it to be called? In the code you've shown, _no_ code calls it, and _no_ notification is configured to call it. Your notifications, if they arrive at all, are configured to call `func keyboardWillAppear` and `func keyboardWillDisappear`, not `func keyboardShown`. – matt Dec 28 '19 at 01:31
  • ok i want height of keyboard based on different devices and orientation . So i thought this way it will work. Can you tell me how to achieve this . – Amit Verma Dec 28 '19 at 06:26
  • Does this answer your question? [How to get height of Keyboard?](https://stackoverflow.com/questions/31774006/how-to-get-height-of-keyboard) – Rehan Ali Khan Dec 29 '19 at 10:57
  • NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil) never getting called . This is my question. what I am doing wrong. Or is there any different approach to get height in keyboard extension. – Amit Verma Dec 29 '19 at 21:48

0 Answers0