[英]UITableView Scrolling/Inset Issue
我有一個帶有多個文本字段的UITableView。 當我單擊文本字段時,我設置了ContentOffset以將文本字段帶到頁面的頂部,但是當鍵盤出現時,我沒有足夠的插圖來這樣做。 因此,我在視圖底部添加了插圖。 這使我可以一直向下滾動,但是現在當我選擇一個字段時,它向下滾動的距離比我設置的setContentOffset還要遠。 我嘗試了不同的內容插入值,但是沒有用。 有沒有辦法解決此內容插入問題?
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 inset設置為0,請執行以下操作:
let contentInsets = UIEdgeInsetsMake(0, 0, adjustmentHeight, 0)
self.editPaymentTableView.contentInset = contentInsets
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.