我正在使用以下代码在显示/隐藏键盘时滚动UITableView及其页脚。它在iOS 10中与我一起运行良好,但是一旦我更新到iOS 11,滚动效果不佳。
代码:
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
}
}
}
希望能解决这个问题。
答案 0 :(得分:0)
添加以下行后(通常在viewDidLoad()
中),tableView的行为将与iOS 10相同:
if #available(iOS 11.0, *) {
self.commentsTableView.contentInsetAdjustmentBehavior = .never
}
答案 1 :(得分:0)
当UITableView
继承UIScrollView
时,您可以使用下面的UIScrollViewDelegate
函数:
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
if scrollView == self.tableView
{
// show/hide Keyboard
}
}
答案 2 :(得分:0)
将断点放在下如果让我自己看到keyboardSize ,对于UIKeyboardFrameBeginUserInfoKey键盘高度可能是0.0
将 UIKeyboardFrameBeginUserInfoKey 更改为 UIKeyboardFrameEndUserInfoKey 帮助我解决了这种情况
所以你的代码:
@objc func keyboardWillShow(_ notification: Notification) {
print("keyboardWillShow")
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] 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
}
}
}
希望它也能解决你的问题
对于 keyboardWillHide 方法,我已将其保留为 UIKeyboardFrameBeginUserInfoKey ,因为它没有问题