简体   繁体   English

类型“Notification.Name”(又名“NSNotification.Name”)没有成员“UIResponder”

[英]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:但是 Xcode 11 不断告诉错误:

Type 'Notification.Name' (aka 'NSNotification.Name') has no member 'UIResponder' in this line:类型“Notification.Name”(又名“NSNotification.Name”)在此行中没有成员“UIResponder”

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.如错误中所见,它没有诸如 Notification.Name UIResponder 之类的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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