我使用以下代码滚动显示/隐藏键盘上的UITableView及其页脚。它在iOS 10中和我一起工作得很好,但是一旦我更新到iOS 11,滚动效果不佳。在显示/隐藏键盘上滚动UITableView iOS11
代码:
func registerNotificationObservers()
{
NotificationCenter.default.addObserver(self, selector: #selector(ArticleDetailsVC.keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(ArticleDetailsVC.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")
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
}
}
}
@objc func keyboardWillHide(_ notification: Notification) {
print("keyboardWillHide")
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
}
}
}
希望解决这个问题。
试过这个,不工作:( – user1553381
我已经尝试过你的代码和我的,tableView将向上移动,当键盘显示在iOS 11上。记住调用你的'registerNotificationObservers()'方法并在'viewDidLoad()'中使用我的代码。 –
我的代码运行良好,当应用程序刚刚启动,但当我隐藏键盘'self.view.endEditing(true)'然后尝试再次显示它,表不滚动 – user1553381