简体   繁体   English

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

[英]iOS Keyboard cover some UITextField in static tableview cell

There are a few UITextField in a cell of my static UITableView. 我的静态UITableView的单元格中有一些UITextField。 Most of them are fine when get focus. 当集中注意力时,大多数都很好。

在此处输入图片说明

在此处输入图片说明

But when some of the fields get focus (when I click for example the Unit No field), the cell jumps and will be covered keyboard like this. 但是,当某些字段成为焦点时(例如,当我单击“单位编号”字段时),单元格将跳转并像这样被键盘覆盖。

在此处输入图片说明

Totally got no clue. 完全没有头绪。 Any idea? 任何想法? Thanks. 谢谢。

Try using this pod https://github.com/hackiftekhar/IQKeyboardManager , It will cover all keyboard scenario. 尝试使用此pod https://github.com/hackiftekhar/IQKeyboardManager ,它将涵盖所有键盘场景。

If you want to use custom code you have to subscribe for UIKeyboardWillShow and UIKeyboardWillHide in viewWillAppear , and unsubscribe in viewWillDisappear . 如果要使用自定义代码,则必须在viewWillAppear中订阅UIKeyboardWillShowUIKeyboardWillHide ,然后在viewWillDisappear中退订。 After that override touchesBegan this will handle your all touches on the screen, you can endEditing in this function. 该覆盖的touchesBegan这将处理屏幕上的所有接触后,你可以在这个函数endEditing。

My Sample code: 我的示例代码:

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)
}

Have you tried IQKeybordManager POD. 您是否尝试过IQKeybordManager POD。 It manages everything automatically. 它自动管理一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM