防止键盘阻塞单元格

时间:2016-11-28 18:09:08

标签: ios swift uitableview uitextfield

我的应用程序显示一个表单,在某些单元格上有文本字段,您可以在其中输入信息。如果单元格朝向屏幕底部,则单元格将被完全阻挡,这意味着您无法查看您正在回答的问题。我的UITableView嵌入在UIViewController中。

我已经查看了许多线程,这些线程提出了这个问题的答案并实现了一些代码。如果我可以使用此方法,我相信我的代码将起作用:

func textFieldDidBeginEditing(_ textField: UITextField) {
    if (self.tableView.contentOffset.y == 0)
    {
        self.tableView.scrollToRow(at: self.tableView.indexPath(for: ), at: UITableViewScrollPosition, animated: true)
    }
}

现在我正试图解决获取活动单元格的问题,以便将其输入self.tableView.indexPath(for: )

我该怎么做呢?这是将视图移到键盘上方的正确方法吗?

5 个答案:

答案 0 :(得分:1)

在您的应用中使用https://github.com/hackiftekhar/IQKeyboardManager。它将保持文本字段和键盘设置正常。

答案 1 :(得分:1)

使用NotificationCenter。

for file in dir/*; do
   read -r -a myline < "$file"
   printf "%s " "${myline[@]%?}"
done

答案 2 :(得分:0)

由我查看这篇文章的答案 iOS Support External Keyboard Without Displaying One

另外,对于tableView,我使用类似的东西来用键盘恰当地显示单元格。

func textFieldDidBeginEditing(_ textField: UITextField) {
    tableView.isScrollEnabled = false
    var path: IndexPath?

    path = theIndexPathOfTheCellWithTheTextField

    if path != nil {
        let rect = CGRect(x: 0, y: 0, width: tableView.frame.width, height: tableView.frame.height)
        tableView.tableFooterView = UIView(frame: rect)

        UIView.animate(withDuration: 0.6, animations: {
            [weak self] in
            self?.tableView.scrollToRow(at: path!, at: .top, animated: false)
        }, completion: nil)
    }
}

func textFieldDidEndEditing(_ textField: UITextField) {
    tableView.tableFooterView = UIView()
    tableView.isScrollEnabled = true
}

如果您有tableView的tableFooterView的UI,则可能需要根据页脚视图的可用大小更改或删除此部分代码。

编辑 - 更多信息: 要创建indexPath,您有几个选项。如果你知道单元格的位置,你可以硬编码:

//This works where neither theRowInt or theSectionInt are negative.
IndexPath(row: theRowInt, section: theSectionInt)

//So this would be the first section/first row
IndexPath(row: 0, section: 0)

下一个方法是将它基于你通过textField找到的单元格:

let textFieldCell = textField.superview.superview.superview as! UITableViewCell
let indexPath = self.tableView.indexPathForCell(textFieldCell)

这可以假设你是正确数量的超级视图。如果将textField直接放入contentView,那么无论如何应该是iOS 7以来的正确数字。如果它崩溃了,而不仅仅是更改.superviews的数量,直到它工作。

还有其他方法,但如果你对它们感兴趣而不是自己研究。关于此事,Here是一个很好的堆栈溢出帖子。

答案 3 :(得分:0)

到目前为止,解决此问题的最简单方法是使用UITableViewController而不是UIViewController和tableView属性。

它会自动为您自动处理它。我很清楚有些情况你不能使用UITableViewController,但如有可能,你应该这样做。使用在常规控制器中嵌入UITableViewController的容器视图控制器,即使在更复杂的情况下也可以实现这一点。

答案 4 :(得分:0)

在viewWillAppear上注册UIKeyboardWillShow和UIKeyboardWillHide通知:

 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil)

然后在键盘上显示你获得键盘的框架并更改tableView的插图

func keyboardWillShow(notification: Notification) {
     guard let keyboardFrame = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
       let convertedFrame = view.convert(keyboardFrame, from: nil)
       tableView.contentInset.bottom = convertedFrame.height
}

然后在keyboardWillHide中将底部插入更改回0,如果需要,将tableView滚动回某行。这会将其滚动回位于tableView顶部的第一行。

 func keyboardWillHide(notification: Notification) {
    tableView.contentInset.bottom = 0
    tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}

这也是一种很好的方法,因为它可以让你的textField委托变得整洁,代码会影响布局。

我相信我在某个地方读到你不需要将某些通知作为观察者删除,其中包括键盘通知,但为了安全起见,你应该将它们放在viewWillDisappear中或根据情况放弃

  deinit {
    NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillHide, object: nil)

     // If you don't have any other notifications you wan't to keep observing you can remove yourself from all observers by using this one line
     // NotificationCenter.default.removeObserver(self)
  } 
相关问题