键盘显示/隐藏通知中心在iOS 11中无效

时间:2017-10-13 19:16:58

标签: 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

       }


    }
 }

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

1 个答案:

答案 0 :(得分:3)

请为观察者尝试此更新语法:

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")
}