iOS 键盘在静态表格 View 单元格中覆盖了一些 UITextField

标签 ios uitableview

我的静态 UITableView 的单元格中有几个 UITextField。当获得焦点时,他们中的大多数都很好。

enter image description here

enter image description here

但是当某些字段获得焦点时(例如当我单击 Unit No 字段时),单元格会跳转并像这样覆盖键盘。

enter image description here

完全没有头绪。任何想法?谢谢。

最佳答案

尝试使用这个 pod https://github.com/hackiftekhar/IQKeyboardManager , 它将涵盖所有键盘场景。

如果您想使用自定义代码,您必须在 viewWillAppear 中订阅 UIKeyboardWillShowUIKeyboardWillHide,并在 viewWillDisappear< 中取消订阅/强>。覆盖 touchesBegan 之后,这将处理您在屏幕上的所有触摸,您可以在此函数中结束编辑。

我的示例代码:

override func viewWillAppear(_ animated: Bool) {

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

}

override func viewWillDisappear(_ animated: Bool) {

    NotificationCenter.default.removeObserver(self)
}

func keyboardWillShow(notification: NSNotification) {
  if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
      UIView.animate(withDuration: 1, animations: {
            self.view.bounds.origin.y += keyboardSize.height
      }
  }
}

func keyboardWillHide(notification: NSNotification) {
    self.view.bounds.origin.y = 0
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    self.view.endEditing(true)
}

关于iOS 键盘在静态表格 View 单元格中覆盖了一些 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46518844/

相关文章:

ios - 访问 LazyTableImages 中的 imageDownloadsInProgress

ios - 分组静态表后台问题

iOS 7 beginUpdates endUpdates 不一致

ios - swift 中的 AES128 加密

javascript - 无法在移动 Safari 中的带有子项的 tabindexed 元素上设置 focus()?

ios - 在 iOS 8 上获取屏幕尺寸

swift - Firebasedata 不填充 tableView

ios - 自动调整表格 View 单元格内的自动调整表格 View 大小

ios - 如何在我的 iOs 应用程序中显示 AppStore 的描述页面?

ios - UITableViewCell 如何与其 UITableView 通信?