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

落花浮王杯 提交于 2019-12-17 09:45:17

问题


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)

Apple document Ref: NSNotification.Name.keyboardDidShowNotification


回答1:


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.




回答2:


Working on swift 4,2

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


}



回答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)


来源:https://stackoverflow.com/questions/52316676/type-nsnotification-name-has-no-member-keyboarddidshownotification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!