28

我在使用 Swift 4.2 时遇到了这个错误

类型“NSNotification.Name”没有成员“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)

在此处输入图像描述

苹果文档参考:NSNotification.Name.keyboardDidShowNotification

4

6 回答 6

95

我相信你现在这样使用它。

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中列为Type Property

于 2018-09-14T05:38:16.990 回答
5

使用 swift 4,2

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


}
于 2018-11-01T19:44:31.707 回答
4

我只使用了UIResponder.keyboardWillHideNotificationexcept using NSNotification.Name.。这适用于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 并选择跳转到定义时。

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

}
于 2019-05-04T08:46:08.697 回答
4

在这个答案中添加了一些小细节:

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
    }
于 2019-12-24T06:29:07.827 回答
0

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

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardDidShow, object: nil)
于 2021-10-27T12:08:03.730 回答
-3

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

于 2021-07-20T08:53:16.627 回答