繁体   English   中英

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

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

我有这个代码:

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

有人知道如何解决吗?

谢谢你的帮助!

利用

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

代替

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

如错误中所见,它没有诸如 Notification.Name UIResponder 之类的调用。

暂无
暂无

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

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