尽管我已经进行了搜索,但我对如何最好地解决这个问题感到困惑。
我有一个 tableView,其中底部单元格是列表的输入,就像苹果提醒的工作方式一样。当列表中的项目太多时,键盘会覆盖列表,我看不到正在输入的内容。
我认为我需要更改表视图的物理大小并确保在键盘显示时它滚动到底部。
有些人说是键盘观察者,但我发现的大多数代码都已经过时,并且在放入 Xcode 时会出现错误。
NSNotificationCenter Swift 3.0 键盘显示和隐藏 https://stackoverflow.com/questions/41718520/nsnotificationcenter-swift-3-0-on-keyboard-show-and-hide
这是我能找到的最好的,但我也听说了一些限制,使用 UITableViewController 而不是嵌入 UITableView 等等......
这是我到目前为止的代码:
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。在这种方法中我是否使导航视图不透明?