UITableView滚动/插入问题
我有一个具有多个文本框的UITableView。当我点击文本框时,我将setContentOffset设置为使文本框位于页面的顶部,但当键盘出现时,我没有足够的插入框来执行此操作。所以我在插图底部添加了插图。这使我可以一直向下滚动,但现在当我选择一个向下滚动的字段时,比我设置的setContentOffset更远。我试过不同的内容插值,但没有任何工作。有没有办法解决这个内容插入问题?UITableView滚动/插入问题
func adjustInsetForKeyboardShow(_ show: Bool, notification: Notification) {
if !keyboardAdjusted || !show {
guard let value = (notification as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
let keyboardFrame = value.cgRectValue
var adjustmentHeight = (keyboardFrame.height + 100) * (show ? 1 : -1)
adjustmentHeight = (adjustmentHeight < 0.0) ? 0.0 : adjustmentHeight
self.editPaymentTableView.contentInset.bottom = adjustmentHeight
self.editPaymentTableView.scrollIndicatorInsets.bottom = adjustmentHeight
}
keyboardAdjusted = show
}
我前一段时间发现这对SO,但无法找到链接到它了,这里要说的是很整齐地处理键盘和tableview中的插图代码。好的是它是用于显示和隐藏的。无需执行其他键盘通知:
public func keyboardWillChangeFrame(notification: NSNotification) {
guard
let userInfo = notification.userInfo,
let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let duration: TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue,
let animationCurveRawNSNumber = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
else {
return
}
let animationCurveRaw = animationCurveRawNSNumber.uintValue
let animationCurve: UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
let offsetY: CGFloat
if endFrame.origin.y >= UIScreen.main.bounds.size.height {
offsetY = 0.0
} else {
offsetY = endFrame.size.height
}
let contentInsets = UIEdgeInsetsMake(0, 0, offsetY, 0)
UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: {
self.tableView.contentInset = contentInsets
self.tableView.scrollIndicatorInsets = contentInsets
}, completion: nil)
}
在解决方案方面,可以取代这两条线路:
var adjustmentHeight = (keyboardFrame.height + 100) * (show ? 1 : -1)
adjustmentHeight = (adjustmentHeight < 0.0) ? 0.0 : adjustmentHeight
有:
var adjustmentHeight = keyboardFrame.height * (show ? 1 : 0)
而且,以确保您'也将top插入到0做:
let contentInsets = UIEdgeInsetsMake(0, 0, adjustmentHeight, 0)
self.editPaymentTableView.contentInset = contentInsets
我试过这个,但它仍然不会让我一直滚动到我的最后一个领域时,键盘显示 –
那么取决于如果您的视图控制器设置为调整顶部滚动插入或不,你可能必须包括当前顶部插入像'let contentInsets = UIEdgeInsetsMake(tableview.contentInset.top,0,adjustmentHeight,0)' –
为了确保您的视图控制器不会更改您的表视图的insets,您可以尝试设置'边缘扩展布局= UIRectEdge(rawValue: UInt(0))'在'viewDidLoad()'中。 –
如果你想使用框架[IQKeyboardManager](https://github.com/hackiftekhar/IQKeyboardManager)可以帮助,易于使用。 –