swift5xcode11uiresponder

Type 'Notification.Name' (aka 'NSNotification.Name') has no member 'UIResponder'


I have this code:

        // MARK: - Listen for Keyboard Events and move the screen up upon keyboard is loaded
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}

@objc func keyboardWillChange(notification: Notification) {

    guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
        return
    }
    if notification.name == Notification.Name.UIResponder.keyboardWillShowNotification ||
        notification.name == Notification.Name.UIKeyboardWillChangeFrame {

                view.frame.origin.y = -keyboardRect.height
    }else {
        view.frame.origin.y = 0
    }


}

But Xcode 11 keeps telling the error:

Type 'Notification.Name' (aka 'NSNotification.Name') has no member 'UIResponder' in this line:

if notification.name == Notification.Name.UIResponder.keyboardWillShowNotification ||

anyone any idea how to fix it?

thx for your help!


Solution

  • Use

    if notification.name == UIResponder.keyboardWillShowNotification ||
        notification.name == UIResponder.keyboardWillChangeFrameNotification {
    

    instead of

    if notification.name == Notification.Name.UIResponder.keyboardWillShowNotification ||
        notification.name == Notification.Name.UIKeyboardWillChangeFrame {
    

    As seen in the error, it does not have a call such as Notification.Name UIResponder.