29

I'm getting this error with Swift 4.2

Type 'NSNotification.Name' has no member 'keyboardDidShowNotification'

Here is my code:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name: NSNotification.Name.keyboardDidShowNotification, object: nil)

Following one was working fine with Swift 4 but not with Swift 4.2

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

enter image description here

Apple document Ref: NSNotification.Name.keyboardDidShowNotification

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • As with your "button type" question... @Krunal - this may be helpful... (not mine, and I haven't tried it... just found via search): https://gist.github.com/benasher44/de0a75a4583e5f57853998142e795776 – DonMag Sep 13 '18 at 15:15
  • @DonMag - Thank you for it. It's very nice. But loosk like a patch/support for Swift4.2. I'm migrating my complete project. – Krunal Sep 13 '18 at 15:20
  • 1
    Sure... but that gist *could* be a decent reference for these types of syntax changes. As far as migrating the complete project, Xcode 10 *does* have a Migration Assistant that may help. Ref: https://swift.org/migration-guide-swift4.2/ – DonMag Sep 13 '18 at 16:08

6 Answers6

99

I believe you use it like this now.

NotificationCenter.default.addObserver(
    self, 
    selector: #selector(self.keyboardDidShow(notification:)), 
    name: UIResponder.keyboardDidShowNotification, object: nil) 

/* You can substitute UIResponder with any of it's subclass */

It is listed in UIResponder doc as a Type Property.

Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
  • 1
    Odd that `.keyboardDidShowNotification` is found in both `UIWindow` and `UIResponder`. – rmaddy Sep 14 '18 at 05:48
  • @rmaddy i actually got this as a suggestion from xcode. But when i go to the docs to the page of the notification, it shows up under `UIWindow`. :/ – Rakesha Shastri Sep 14 '18 at 05:49
  • Oddly, both versions (UIWindow and UIResponder) compile. – rmaddy Sep 14 '18 at 05:50
  • @rmaddy i found out why i think. _"Many key objects are also responders, including the UIApplication object, UIViewController objects, and all UIView objects (which **includes UIWindow**)"_ https://developer.apple.com/documentation/uikit/uiresponder – Rakesha Shastri Sep 14 '18 at 05:50
  • `.keyboardDidShowNotification` must actually be declared in `UIResponder` because I can do `UIView.keyboardDidShowNotification` or any class that ultimately descends from `UIResponder`. – rmaddy Sep 14 '18 at 05:53
  • i am really confused why observer UIResponder.keyboardWillShowNotification not getting called . I am using it inside KeyboardViewController of keyboard Extension. – Amit Verma Dec 28 '19 at 01:03
5

Working on swift 4,2

 func bindToKeyboard(){
    NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name:
        UIApplication.keyboardWillChangeFrameNotification
        , object: nil)


}
Daniel
  • 161
  • 1
  • 5
5

I have used just the UIResponder.keyboardWillHideNotification except using NSNotification.Name. . This is working in SWIFT 5

NotificationCenter.default.addObserver(self, selector: #selector(KeyboardLayoutConstraint.keyboardWillShowNotification(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHideNotification(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)

When I command-click on keyboardWillShowNotification and select jump to definition.

extension UIResponder {

    
    public class let keyboardWillShowNotification: NSNotification.Name

    public class let keyboardDidShowNotification: NSNotification.Name

    public class let keyboardWillHideNotification: NSNotification.Name

    public class let keyboardDidHideNotification: NSNotification.Name

}
4

Some small details added to this answer:

func setupViews(){
            // Keyboard notification observers
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil) 
    }


    @objc func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            // Do something
        }
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        // Do something
    }
fullmoon
  • 8,030
  • 5
  • 43
  • 58
0

After update in Swift 4.2 you directly use .UIKeyboardDidShow and .UIKeyboardDidHide

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardDidShow, object: nil)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-3

@Krunal.... remove 'NSNotification.Name' it will be working

  • 1
    This is the same solution as in [this other answer](https://stackoverflow.com/a/55980922/2227743). – Eric Aya Jul 20 '21 at 09:57