问题描述
尽管我进行了搜索,但对如何最好地解决这一问题感到困惑.
Although I've searched, I'm confused about how best to approach this.
我有一个tableView,底部的单元格是列表的输入,与苹果提醒的工作方式相同.当列表中的项目过多时,键盘将覆盖列表,而我看不到正在输入的内容.
I have a tableView where the bottom cell is an input to the list, in the same way apple reminders work. When there are too many items on the list, the keyboard covers the list and I can't see what I'm typing.
我认为我需要更改表格视图的物理尺寸,并确保在键盘显示时将其滚动到底部.
My thought it I need to change the physical size of the table view and ensure it is scrolled to the bottom when the keyboard shows.
有些人说过键盘观察员,但是我发现的大多数代码都已过时,并且在放入Xcode时出错.
Some have said keyboard observers but the majority of code I've found for this is out of date and errors when put into Xcode.
在键盘上显示和隐藏的NSNotificationCenter Swift 3.0
这是我能找到的最好的方法,但我也听说过有关约束的问题,它使用UITableViewController而不是嵌入UITableView等...
This is the best I can find but I'm also hearing about contraints, using a UITableViewController instead of embedding a UITableView and so on ...
这是我到目前为止的代码:
This is the code I have so far:
NotificationCenter.default.addObserver(self, selector: #selector(EntryViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(EntryViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
@objc func keyboardWillShow(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
print("notification: Keyboard will show")
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= keyboardSize.height
}
}
}
@objc func keyboardWillHide(notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y += keyboardSize.height
}
}
}
我认为这会使整个视图向上移动,这意味着安全区域(例如导航栏等)在其下方具有TableView.我是否可以通过这种方法使Navigationview不透明?
This moves the whole view up I think, which means safeareas (such as navigation bar and so on) have the TableView underneath them. Do I make the navigationview non-transparent in this approach?
推荐答案
一种解决方案(我有时使用)只是在键盘出现/消失时更改 tableView
的内容偏移量.我相信这将在您的实例中起作用,而不是像您提到的 UIViewController
是 UITableViewController
那样改变tableView的约束.我的建议请参见以下代码,希望对您有所帮助!
One solution (which I sometimes use) is simply change the content offset of the tableView
when the keyboard appears/disappears. I believe this would work in your instance as opposed to varying the tableView's constraints as you mentioned your UIViewController
is a UITableViewController
. Please see the below code for my suggestion, hope this helps!
处理通知:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(EntryViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(EntryViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
操作:
@objc func keyboardWillShow(notification: Notification) {
if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
print("Notification: Keyboard will show")
tableView.setBottomInset(to: keyboardHeight)
}
}
@objc func keyboardWillHide(notification: Notification) {
print("Notification: Keyboard will hide")
tableView.setBottomInset(to: 0.0)
}
扩展名:
extension UITableView {
func setBottomInset(to value: CGFloat) {
let edgeInset = UIEdgeInsets(top: 0, left: 0, bottom: value, right: 0)
self.contentInset = edgeInset
self.scrollIndicatorInsets = edgeInset
}
}
这篇关于Swift 4.2在键盘显示时使tableView的底部向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!