简体   繁体   English

UITableView滚动/插入问题

[英]UITableView Scrolling/Inset Issue

I have a UITableView with multiple textfields. 我有一个带有多个文本字段的UITableView。 When I click on a textfield, I setContentOffset to bring the textfield to the top of the page, but I do not have enough inset to do so when the keyboard appears. 当我单击文本字段时,我设置了ContentOffset以将文本字段带到页面的顶部,但是当键盘出现时,我没有足够的插图来这样做。 So I added inset to the bottom of the view. 因此,我在视图底部添加了插图。 This allows me to scroll all the way down but now when I select a field it scrolls down too far, further than the setContentOffset that I set. 这使我可以一直向下滚动,但是现在当我选择一个字段时,它向下滚动的距离比我设置的setContentOffset还要远。 I've tried different content inset values, but none work. 我尝试了不同的内容插入值,但是没有用。 Is there a way to fix this content inset issue? 有没有办法解决此内容插入问题?

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
    }

I've found this on SO some time ago, but can't find the link to it anymore, here is the code that very neatly handles the keyboard and the insets of the tableview. 我已经在SO上找到了它,但是现在找不到它的链接,这里的代码非常巧妙地处理了键盘和tableview的插图。 Nice thing is it's for showing and hiding. 好东西是用于显示和隐藏。 No need to implement other keyboard notifications: 无需实施其他键盘通知:

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)
}

In terms of your solution, you can replace these two lines: 就您的解决方案而言,您可以替换以下两行:

var adjustmentHeight = (keyboardFrame.height + 100) * (show ? 1 : -1)
adjustmentHeight = (adjustmentHeight < 0.0) ? 0.0 : adjustmentHeight

with: 有:

var adjustmentHeight = keyboardFrame.height * (show ? 1 : 0)

And to be sure that you're also setting top inset to 0 do: 并且要确保您还将top inset设置为0,请执行以下操作:

let contentInsets = UIEdgeInsetsMake(0, 0, adjustmentHeight, 0)
self.editPaymentTableView.contentInset = contentInsets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM