简体   繁体   English

IOS 13 中的滚动问题

[英]Scroll Issue in IOS 13

In order to achieve scroll To bottom for a table view, I am using the below code.为了实现表格视图的滚动到底部,我使用了下面的代码。

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

This is working perfectly fine for all the devices whose versions are below 13, but in ios 13 it is not scrolling completely to last cell , it is stopping in between the last cell (approximate 40 pixel from bottom).这对于版本低于 13 的所有设备都非常有效,但在 ios 13 中,它没有完全滚动到最后一个单元格,而是停在最后一个单元格之间(距底部约 40 像素)。

I also tried alternate ways by我也尝试了替代方法

  1. setting content Offset设置内容偏移
  2. setting the scroll to visible rect all将滚动设置为可见 rect all
  3. having a delay for 1.0 seconds延迟 1.0 秒

but all of these having the same behaviour, not scrolling completely.但所有这些都具有相同的行为,而不是完全滚动。

If you're facing this issue because of having different cells with different heights then below code will probably work for you:如果您因为具有不同高度的不同单元格而面临此问题,那么下面的代码可能适合您:

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

Try this尝试这个

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

Thank Shivam Pokhriyal感谢Shivam Pokhriyal

It helps me work properly on iOS 13, But I don't know exactly why它帮助我在 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
        }
    }
}

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

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