Scroll to the bottom of the tableView before view is on screen


#1

Hello,

I’m currently building an app which has a chat integrated.

I want the default behavior of iMessage & WhatsApp where you tap on one conversation, you land automatically to the bottom of the tableView/collectionView.

I don’t want the user to notice the scrolling to bottom of the tableView/collectionView.

Thank you in advance.


#2

You should be able to achieve this by scrolling to the row without an animation:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    tableView.scrollToRow(at: lastIndexPath(), at: .bottom, animated: false)
}

private func lastIndexPath() -> IndexPath {
    let row = tableView.numberOfRows(inSection: 0) - 1
    return IndexPath(row: row, section: 0)
}

You may have to add some checks in here to avoid scrolling to the bottom every time they come back to this screen (perhaps if they scrolled up manually) – but putting this code in viewDidLoad is too early.


#3

Hey Ben, thank you,

This is the current implementation I have, but I notice that even when placing this code in the viewWillAppear, the user still get a glimpse of the tableView scrolling to the bottom and this is the effect I want to avoid.


#4

The way we implemented something like this is to put new messages at the top, and flip the table with
tableView.transform = CGAffineTransform(scaleX: 1, y: -1)

Now your top cells are at the bottom. And, of course, they’re upside down. So, you flip them too.
cell.transform = CGAffineTransform(scaleX: 1, y: -1)


#5

Awesome,

Thank you very much Kyle for your response. After a few tricks, like reordering the list of messages to ascending order, it works the way I expect.

Cheers :slight_smile:


#6

:exploding_head:

That is amazing.

(I am curious why scrolling to the bottom non-animated doesn’t work though.)


#7

It does indeed work since it scrolls to the bottom of the tableView, but the user still sees for 1s the tableView jumping to the bottom of the list even though I call the method on viewWillAppear.

But Kyle’s solution is definitely the one I was looking for.

Anyway, thank you both for your responses and your time.

Cheers