简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM