简体   繁体   中英

iOS 13 can't scroll to bottom as soon as UISearchController opens

In iOS 13, when you open UISearchController, it displays the tableView content immediately. I want to be able to scroll all the way to the bottom (say we have one hundred items) and show the users the content in the bottom of the tableView as soon as UISearchController is presented. But this is impossible in iOS 13. setContentOffset or scrollToRowAtIndexPathh do not work fully when I call either one of them in willPresentSearchController or in didPresentSearchController. I cannot believe it even doesn't work in didPresentController. But I can call either of them 0,3 seconds after didPresentController. and it would scroll all the way to the bottom. But I do not want my users to wait 0.3 seconds after presenting UISearchController to scroll to the bottom. I want it to already have scrolled to bottom immediately when UISearchController is shown without any flickers or animations.

I am using a UISearchBar to present UISearchController. Once you tap UISearchBar it moves up to the navigation bar and fades in the full screen tableView for search.

I find it impossible to scroll to the bottom before the content of UISearchController is shown. Maybe the only luck is wait for Apple to fix. I would love to hear if someone has a solution to this problem. I was able to force this to happen in iOS 11. In iOS 12, I had do an animated scroll after UISearchController was presented. In iOS 13, it seems impossible all together other than waiting 0.3 seconds.

Try this

Swift:

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

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(), ^{
                [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated];
            });
        } else {
            // Fallback on earlier versions
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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