Krunal
Krunal

Reputation: 79666

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

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

Upvotes: 29

Views: 37076

Answers (6)

Jagjeet Gandhi
Jagjeet Gandhi

Reputation: 11

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

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardDidShow, object: nil)

Upvotes: 1

Kulpreet Singh
Kulpreet Singh

Reputation: 1

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

Upvotes: -3

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

}

Upvotes: 5

Badr Bujbara
Badr Bujbara

Reputation: 8671

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
    }

Upvotes: 4

Rakesha Shastri
Rakesha Shastri

Reputation: 11242

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.

Upvotes: 100

Daniel
Daniel

Reputation: 161

Working on swift 4,2

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


}

Upvotes: 5

Related Questions