Swift 4.2 键盘显示时tableView底部上移
Swift 4.2 Make bottom of tableView move up when keyboard shows
尽管我进行了搜索,但我对如何最好地解决这个问题感到困惑。
我有一个 tableView,其中底部单元格是列表的输入,与苹果提醒的工作方式相同。当列表中的项目太多时,键盘会覆盖列表,我看不到我正在输入的内容。
我想我需要更改 table 视图的物理大小并确保它在键盘显示时滚动到底部。
有些人说键盘观察者,但我发现的大部分代码都已过时并且在放入 Xcode 时出错。
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。在这种方法中我是否使导航视图不透明?
一个解决方案(我有时会使用)是简单地更改键盘 appears/disappears 时 tableView
的内容偏移量。我相信这会在您的实例中起作用,而不是像您提到的 UIViewController
是 UITableViewController
那样改变 tableView 的约束。请参阅下面的代码以获取我的建议,希望对您有所帮助!
处理通知:
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
}
}
尽管我进行了搜索,但我对如何最好地解决这个问题感到困惑。
我有一个 tableView,其中底部单元格是列表的输入,与苹果提醒的工作方式相同。当列表中的项目太多时,键盘会覆盖列表,我看不到我正在输入的内容。
我想我需要更改 table 视图的物理大小并确保它在键盘显示时滚动到底部。
有些人说键盘观察者,但我发现的大部分代码都已过时并且在放入 Xcode 时出错。
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。在这种方法中我是否使导航视图不透明?
一个解决方案(我有时会使用)是简单地更改键盘 appears/disappears 时 tableView
的内容偏移量。我相信这会在您的实例中起作用,而不是像您提到的 UIViewController
是 UITableViewController
那样改变 tableView 的约束。请参阅下面的代码以获取我的建议,希望对您有所帮助!
处理通知:
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
}
}