1

I'm writing a custom keyboard extension, and although I am registering as an observer of the keyboard hide / show notifications, my breakpoints never hit and my log statements never print. Is there something different that happens with NSNotifications inside of a UIInputViewController subclass or am I doing something wrong?

import UIKit

class KeyboardViewController: UIInputViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Perform custom UI setup here
    let keysView:UIView = NSBundle.mainBundle().loadNibNamed("KeyboardView", owner: self, options:nil)[0] as UIView
    keysView.frame = self.inputView.frame
    keysView.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.inputView.addSubview(keysView)
    self.inputView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[keysView]|", options: nil, metrics: nil, views:["keysView":keysView]))
    self.inputView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[keysView]|", options: nil, metrics: nil, views:["keysView":keysView]))
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    // Listen for changes to keyboard visibility so that we can adjust the view accordingly.
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self, selector: "handleKeyboardWillShowNotification:", name: UIKeyboardWillShowNotification, object: nil)
    notificationCenter.addObserver(self, selector: "handleKeyboardWillHideNotification:", 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)
}

func handleKeyboardWillHideNotification(notification: NSNotification) {
    NSLog("keyboardWillHide")
}

func handleKeyboardWillShowNotification(notification: NSNotification) {
    NSLog("keyboardWillShow")
}

}
jeffctown
  • 176
  • 1
  • 1
  • 8
  • Do you need to listen to these events? The UIInputViewController notifies you when the keyboard should be displayed or dismissed. – nurnachman Oct 03 '14 at 03:17
  • `UIKeyboardWillShowNotification`,`UIKeyboardWillHideNotification` is not availble in iOS keyboard-extension. Check `viewWillAppear`,`viewWillDissappear` instead. – Tony Feb 09 '15 at 03:26
  • I am also facing the same problem, The only difference being my `UIInputViewController` subclass is listening to a custom notification. – manish_kumar Sep 10 '15 at 10:34

0 Answers0