【问题标题】:Type 'Notification.Name' (aka 'NSNotification.Name') has no member 'UIResponder'类型“Notification.Name”(又名“NSNotification.Name”)没有成员“UIResponder”
【发布时间】:2020-08-11 21:28:48
【问题描述】:

我有这个代码:

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

有人知道怎么解决吗?

感谢您的帮助!

【问题讨论】:

    标签: swift5 xcode11 uiresponder


    【解决方案1】:

    使用

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

    而不是

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

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

    【讨论】:

    • Rica ederim。快乐编码:)
    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 2019-09-17
    • 2019-02-18
    • 2019-02-18
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多