Тип «Notification.Name» (он же «NSNotification.Name») не имеет члена «UIResponder» - PullRequest
1 голос
/ 28 апреля 2020

У меня есть этот код:

        // 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
    }


}

Но Xcode 11 продолжает сообщать об ошибке:

Тип «Notification.Name» (он же «NSNotification.Name») не имеет участник 'UIResponder' в этой строке:

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

Кто-нибудь знает, как это исправить?

спасибо за вашу помощь!

1 Ответ

1 голос
/ 28 апреля 2020

Используйте

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

вместо

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

Как видно из ошибки, у него нет вызова, такого как Notification.Name UIResponder.

...