Skip to content

Fixes scrollToBottom method to properly handle calls made early in the view lifecycle - #1110

Merged
nathantannar4 merged 3 commits into
MessageKit:developmentfrom
marcetcheverry:3.0.0-swift5-scrollToBottom
Jul 8, 2019
Merged

nathantannar4 merged 3 commits into
MessageKit:developmentfrom
marcetcheverry:3.0.0-swift5-scrollToBottom

Conversation

@marcetcheverry

@marcetcheverry marcetcheverry commented Jun 25, 2019 •

Copy link
Copy Markdown

What does this implement/fix? Explain your changes.
—————————————————————————
Wanting to implement a default "scrolled to bottom" appearance I did something like this

    private var hasAppearedOnce = false
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if !hasAppearedOnce {
            messagesCollectionView.scrollToBottom()
            hasAppearedOnce = true
        }
    }

(Let me know if there is a better way to do this)

However, I found that collectionViewContentHeight was often 0 in scrollToBottom, leading to this code not working unless it was called twice.

This was the old implementation:

    public func scrollToBottom(animated: Bool = false) {
        let collectionViewContentHeight = collectionViewLayout.collectionViewContentSize.height

        performBatchUpdates(nil) { _ in
            self.scrollRectToVisible(CGRect(0.0, collectionViewContentHeight - 1.0, 1.0, 1.0), animated: animated)
        }
    }

The documentation for performBatchUpdates states:

If the collection view's layout is not up to date before you call this method, a reload may occur.

Therefore, moving the capturing of the collection view height to inside the block fixes the issue, as performBatchUpdates will ensure all basic layout is done and we have accurate data to work off.

Whatever you think of the implementation of the automatic scroll to bottom on first appearance, the fix for this issue is still valid, as calling this method before the first reloadData right now does not work, and this PR fixes that.

Any other comments?

Not sure which branch to submit too, but I am using Swift 5.

Where has this been tested?

Devices/Simulators: iPhone XS

iOS Version: 12.2

Swift Version: Swift 5

MessageKit Version: 3.0.0-swif5

👻

@marcetcheverry marcetcheverry changed the title Fix scrollToBottom not working properly when called early Fixes scrollToBottom method to properly handle calls made early in the view lifecycle Jun 25, 2019
@nathantannar4

Copy link
Copy Markdown
Member

Thanks @marcetcheverry , your reasoning makes sense. I will test this when I return from holiday.

Sent with GitHawk

@nathantannar4
nathantannar4 changed the base branch from 3.0.0-swift5 to development July 8, 2019 01:38
@nathantannar4
nathantannar4 merged commit 2029800 into MessageKit:development Jul 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants