简体   繁体   中英

Notification centre for keyboard show/hide not working in iOS 11

Notification center register for keyboard show/hide was working for my application, Once I updated to iOS 11 or higher, the keyboard notification centre are not working?

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

       }


    }
 }

What should I need to do? Thanks in advance.

Try this updated syntax instead for your observers:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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