类型 'NSNotification.Name' 没有成员 'keyboardDidShowNotification'

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

我在使用 Swift 4.2

时遇到此错误

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

这是我的代码:

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

下面的一个在 Swift 4 上运行良好,但在 Swift 4.2

上运行不正常
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

Apple 文档参考:NSNotification.Name.keyboardDidShowNotification

相信你现在是这样用的

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

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

它在 UIResponder doc 中列为 类型 属性

正在处理 swift 4,2

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


}

除了 NSNotification.Name. 之外,我只使用了 UIResponder.keyboardWillHideNotification 。这适用于 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)

当我命令单击 keyboardWillShowNotification 并 select 跳转到定义时。

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

}

此答案中添加了一些小细节:

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
    }

@Krunal.... 删除 'NSNotification.Name' 它将起作用

在 Swift 4.2 中更新后,您可以直接使用 .UIKeyboardDidShow.UIKeyboardDidHide

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