ios - 键盘显示/隐藏的通知中心在 iOS 11 中不起作用

标签 ios swift keyboard ios11 nsnotificationcenter

注册键盘显示/隐藏的通知中心对我的应用程序有效,更新到 iOS 11 或更高版本后,键盘通知中心无法正常工作?

func registerNotificationObservers()
{
    NotificationCenter.default.addObserver(self, selector: #selector(ArticleDetailsVC.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(ArticleDetailsVC.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

   }

func removeNotificationObservers()
{

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)


}

func keyboardWillShow(notification: NSNotification)
{
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
    {
        if self.commentsTableView.frame.origin.y == 0{
            print("keyboardWillShow ..")
            self.tableViewFooter.frame.origin.y -= keyboardSize.height - 50
            self.commentsTableView.frame.origin.y -= keyboardSize.height

        }


    }

}


func keyboardWillHide(notification: NSNotification)
{
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
    {
        if self.commentsTableView.frame.origin.y != 0{

            print("keyboardWillHide ..")
            self.tableViewFooter.frame.origin.y += keyboardSize.height + 50
            self.commentsTableView.frame.origin.y += keyboardSize.height

       }


    }
 }

我需要做什么? 提前致谢。

最佳答案

为您的观察者尝试这个更新的语法:

func registerNotificationObservers() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil)
}

func removeNotificationObservers() {
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil)
}

@objc func keyboardWillShow(_ notification: Notification) {
    print("keyboardWillShow")
}


@objc func keyboardWillHide(_ notification: Notification) {
    print("keyboardWillHide")
}

关于ios - 键盘显示/隐藏的通知中心在 iOS 11 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46736677/

相关文章:

JavaFX 虚拟键盘闪烁

javascript - 按键时无法从 iframe 失去焦点

ios - UICollectionView重新加载数据Swift 4

ios - fatal error : unexpectedly found nil , 我定义了一个手势,这个方法是手势绑定(bind)的方法

ios - Swift - 子 Controller 导致 'Receiver has no segue with identifier'

ios - Swift - 无法显示导航栏

ios - 自动布局 - UILabel 在方向更改后宽度缩小,这是由另一个应该自动调整字体大小的 UILabel 引起的

ios - 升级到 XCode 13(和 iOS 15)后选项卡和导航栏发生变化

swift - 在运行时获取动态类型的单元格不起作用

winapi - 来自 Windows 的低级键盘输入