【问题标题】:Scrolling UITableView on show/hide keyboard iOS11在显示/隐藏键盘iOS11上滚动UITableView
【发布时间】:2018-03-26 09:33:54
【问题描述】:

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

    }
}

希望能解决这个问题。

【问题讨论】:

    标签: ios swift uitableview keyboard


    【解决方案1】:

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

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

    【讨论】:

    • 我已经尝试了你和我的代码,在iOS 11上显示键盘时tableView会向上移动。记得调用你的registerNotificationObservers()方法并在viewDidLoad()中挖掘代码。
    • 我的代码在应用刚启动时运行良好,但是当我通过self.view.endEditing(true) 隐藏键盘然后尝试再次显示时,表格无法滚动
    【解决方案2】:

    由于 UITableView 继承 UIScrollView ,你在下面使用这个 UIScrollViewDelegate 函数:

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

    【讨论】:

      【解决方案3】:

      if let 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 因为它没有问题

      【讨论】:

        猜你喜欢
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-21
        • 1970-01-01
        相关资源
        最近更新 更多