2017-10-14 95 views
1

我使用以下代码滚动显示/隐藏键盘上的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 
     } 

    } 
} 

希望解决这个问题。

回答

0

(在viewDidLoad()正常)添加以下行后,您的tableView的行为将是相同的iOS 10的:

if #available(iOS 11.0, *) { 
     self.commentsTableView.contentInsetAdjustmentBehavior = .never 
    } 
+0

试过这个,不工作:( – user1553381

+0

我已经尝试过你的代码和我的,tableView将向上移动,当键盘显示在iOS 11上。记住调用你的'registerNotificationObservers()'方法并在'viewDidLoad()'中使用我的代码。 –

+0

我的代码运行良好,当应用程序刚刚启动,但当我隐藏键盘'self.view.endEditing(true)'然后尝试再次显示它,表不滚动 – user1553381

0

由于UITableView继承UIScrollView,您可以使用下面这个UIScrollViewDelegate功能:

func scrollViewDidScroll(_ scrollView: UIScrollView) 
{ 
     if scrollView == self.tableView 
     { 
      // show/hide Keyboard 
     } 
} 
0

将断点放在之后如果让keyboardSize我有时看到,对于UIKeyboardFrameBeginUserInfoKey键盘高度可能是0.0

更改UIKeyboardFrameBeginUserInfoKeyUIKeyboardFrameEndUserInfoKey帮助我在这种情况下

所以,你的代码:

@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方法,我已经用UI KeyboardFrameBeginUserInfoKey,因为它没有问题