2017-10-13 61 views
0

键盘显示/隐藏通知中心注册表正在为我的应用程序工作,一旦我更新到iOS 11或更高版本,键盘通知中心不工作?键盘显示/隐藏通知中心不适用于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 

     } 


    } 
} 

我该怎么做? 在此先感谢。

+0

显示使用you're的代码。 –

+0

我添加了代码 – user1553381

+0

,您需要在keyboardWillShow和keyboardWillHide方法前添加@objc。 '@objc func keyboardWillHide(_ notification:Notification)'和'@objc func keyboardWillShow(_ notification:Notification)' –

回答

2

试试这个更新过的语法,而不是你的观察:

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

我尝试了你的代码,它适用于我,现在的问题是滚动表和页脚不起作用,我的意思是这个代码'self.tableViewFooter.frame.origin.y - = keyboardSize.height - 50 self.commentsTableView.frame.origin.y - = keyboardSize.height' – user1553381

+0

@ user1553381,这是逻辑不知道哪里出了问题。问题是显示/隐藏。也许你可以为此提出一个新问题或添加更多相关的代码。 –

+0

好的我会这么做的,这段代码在ios 10中和我一起工作的很好,但是一旦我更新到ios 11,它不会很好地滚动,我绝望了。 – user1553381