swift 出现键盘时 UITableView 向上滚动,但我看不到顶部单元格
swift UITableView scrolls up when keyboard appears but I can't see the top cells
我有一个带有 TableView 和 textView 的 ViewController。我正在使用以下代码,以便在出现键盘时将 textView 和 Tableview 向上推。
绑定到键盘
extension UIView {
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector:
#selector(UIView.keyboardWillChange(_:)), name:
UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification) {
let duration = notification.userInfo!
[UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey]
as! UInt
let curFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey]
as! NSValue).cgRectValue
let targetFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey]
as! NSValue).cgRectValue
let deltaY = targetFrame.origin.y - curFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options:
UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
},completion: {(true) in
self.layoutIfNeeded()
})
}
}
我遇到的问题是 tableView 中的顶部单元格,我无法向下滚动以显示它们,除非我关闭键盘。我一直在寻找可能的代码示例来解决这个问题,但我似乎找不到合适的解决方案。非常感谢任何输入。
解决方案
已将观察者添加到 viewDidLoad
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
这已添加到 class
@objc func adjustForKeyboard(notification: Notification) {
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEndFrame = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == UIResponder.keyboardWillHideNotification {
tableView.contentInset = .zero
} else {
//Modify the top insets to 350 and the height of the keyboard > 10 ? 10 : 10
//to eliminate the gap between the bottom cell and the textView
tableView.contentInset = UIEdgeInsets(top: 350, left: 0, bottom: keyboardViewEndFrame
.height > 10 ? 10 : 10, right: 0)
}
tableView.scrollIndicatorInsets = tableView.contentInset
}
希望这对任何人都有帮助。
您正在更改 table 视图的 frame
,但您应该修改 contentOffset
或 contentInsets
属性。
如果您移动框架,整个 table 将移出屏幕,因此顶部单元格也会移出屏幕。
有很多(我的意思是很多很多)教程可能对您有帮助,请参见例如https://www.hackingwithswift.com/example-code/uikit/how-to-adjust-a-uiscrollview-to-fit-the-keyboard
还有多种解决方案可以在例如github.
你也可以在你的项目中使用IQKeyboardManager cocoapod
我有一个带有 TableView 和 textView 的 ViewController。我正在使用以下代码,以便在出现键盘时将 textView 和 Tableview 向上推。
绑定到键盘
extension UIView {
func bindToKeyboard(){
NotificationCenter.default.addObserver(self, selector:
#selector(UIView.keyboardWillChange(_:)), name:
UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification) {
let duration = notification.userInfo!
[UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey]
as! UInt
let curFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey]
as! NSValue).cgRectValue
let targetFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey]
as! NSValue).cgRectValue
let deltaY = targetFrame.origin.y - curFrame.origin.y
UIView.animateKeyframes(withDuration: duration, delay: 0.0, options:
UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
self.frame.origin.y += deltaY
},completion: {(true) in
self.layoutIfNeeded()
})
}
}
我遇到的问题是 tableView 中的顶部单元格,我无法向下滚动以显示它们,除非我关闭键盘。我一直在寻找可能的代码示例来解决这个问题,但我似乎找不到合适的解决方案。非常感谢任何输入。
解决方案
已将观察者添加到 viewDidLoad
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
这已添加到 class
@objc func adjustForKeyboard(notification: Notification) {
guard let keyboardValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardScreenEndFrame = keyboardValue.cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == UIResponder.keyboardWillHideNotification {
tableView.contentInset = .zero
} else {
//Modify the top insets to 350 and the height of the keyboard > 10 ? 10 : 10
//to eliminate the gap between the bottom cell and the textView
tableView.contentInset = UIEdgeInsets(top: 350, left: 0, bottom: keyboardViewEndFrame
.height > 10 ? 10 : 10, right: 0)
}
tableView.scrollIndicatorInsets = tableView.contentInset
}
希望这对任何人都有帮助。
您正在更改 table 视图的 frame
,但您应该修改 contentOffset
或 contentInsets
属性。
如果您移动框架,整个 table 将移出屏幕,因此顶部单元格也会移出屏幕。
有很多(我的意思是很多很多)教程可能对您有帮助,请参见例如https://www.hackingwithswift.com/example-code/uikit/how-to-adjust-a-uiscrollview-to-fit-the-keyboard
还有多种解决方案可以在例如github.
你也可以在你的项目中使用IQKeyboardManager cocoapod