Тип 'NSNotification.Name' не имеет имени 'keyboardDidShowNotification' - программирование
Подтвердить что ты не робот

Тип 'NSNotification.Name' не имеет имени 'keyboardDidShowNotification'

Я получаю эту ошибку с 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)

enter image description here

Ссылка на документ Apple: NSNotification.Name.keyboardDidShowNotification

4b9b3361

Ответ 1

Я верю, что вы используете это сейчас.

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

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

Он указан в документе UIResponder как свойство типа.

Ответ 2

Работаю на swift 4,2

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


}

Ответ 3

Я использовал только UIResponder.keyboardWillHideNotification за исключением использования 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)