iOS 13 中的滚动问题

huangapple go评论85阅读模式
英文:

Scroll Issue in IOS 13

问题

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

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

这对于所有版本低于iOS 13的设备都可以完美工作,但在iOS 13上,它无法完全滚动到最后一个单元格,而是在最后一个单元格之间停止(距离底部大约40像素)。

我还尝试了以下替代方法:

  1. 设置内容偏移量
  2. 设置滚动到可见矩形区域
  3. 延迟1.0秒

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

英文:

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

I also tried alternate ways by

  1. setting content Offset
  2. setting the scroll to visible rect all
  3. having a delay for 1.0 seconds

but all of these having the same behaviour, not scrolling completely.

答案1

得分: 4

如果您面临此问题是因为具有不同高度的不同单元格,则以下代码可能适用于您:

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)
    }
}
英文:

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

答案2

得分: 1

尝试这样做

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)
    }
}
英文:

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

答案3

得分: 1

感谢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 {
            // 适用于较早版本的回退
        }
    }
}
英文:

Thank Shivam Pokhriyal

It helps me work properly on iOS 13, But I don't know exactly why

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

huangapple
  • 本文由 发表于 2020年1月3日 16:08:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定