IOS 13 中的滚动问题

Scroll Issue in IOS 13

为了实现 table 视图的滚动到底部,我使用以下代码。

extension UITableView {

func scrollToBottom(){
        let indexPath = IndexPath(
                row: self.numberOfRows(inSection:  self.numberOfSections -1) - 1, 
                section: self.numberOfSections - 1)
        self.scrollToRow(at: indexPath, at: .bottom, animated: true)
    }
}

这对于版本低于 13 的所有设备都工作得很好,但在 ios13 中,它没有完全滚动到最后一个单元格,而是在最后一个单元格之间停止(距离大约 40 像素)底部)。

我也尝试了

的替代方法
  1. 设置内容偏移量
  2. 将滚动设置为可见的矩形
  3. 延迟 1.0 秒

但所有这些都具有相同的行为,没有完全滚动。

试试这个

func scrollToBottom(){
    DispatchQueue.main.async {
        let indexPath = IndexPath(row: self.yourDataSourceArray-1, section: self.numberOfSections - 1)
        self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
    }
}

如果您因为不同单元格的高度不同而遇到此问题,那么以下代码可能适合您:

private func moveTableViewToBottom(indexPath: IndexPath) {
    tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
    }
}

感谢Shivam Pokhriyal

它帮助我在 iOS 13 上正常工作,但我不知道为什么

Swift:

private func moveTableViewToBottom(indexPath: IndexPath) {
    tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
        if #available(iOS 13.0, *) {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
        self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
        }
    }
}

OC:

- (void)scrollToBottomAnimated:(BOOL)animated {

NSInteger rows = [self.tableView numberOfRowsInSection:0];
    if (rows > 0) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rows-1 inSection:0];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated];
        if (@available(iOS 13.0, *)) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                NSInteger rows = [self.tableView numberOfRowsInSection:0];
                if (rows > 0) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rows-1 inSection:0];
                    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated];
                }
            });
        } else {
            // Fallback on earlier versions
        }
    }
}