ios 类型“NSNotification.Name”没有成员“keyboardDidShowNotification”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52316676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:47:42  来源:igfitidea点击:

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

ioskeyboard-eventsnsnotificationcenterswift4.2

提问by Krunal

I'm getting this error with Swift 4.2

我在 Swift 4.2 中遇到此错误

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

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

以下一个在 Swift 4 上运行良好,但在 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

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

回答by Rakesha Shastri

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 UIResponderdocas a Type Property.

它在UIResponderdoc 中列为Type Property

回答by Daniel

Working on swift 4,2

在 swift 4,2 上工作

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


}

回答by Rakshitha Muranga Rodrigo

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

我只使用了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)

回答by Badr

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
    }