Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scroll to top doesn't work correctly #389

Open
arashetm opened this issue Jan 19, 2020 · 11 comments
Open

scroll to top doesn't work correctly #389

arashetm opened this issue Jan 19, 2020 · 11 comments

Comments

@arashetm
Copy link

arashetm commented Jan 19, 2020

Describe the bug
scroll to the top doesn't work correctly.

To Reproduce
create a tableView including too many items.
scroll to the middle of tableView and then call this:

if let navigationController = self.navigationController as? ScrollingNavigationController {
    navigationController.showNavbar(animated: true, duration: 0.2, scrollToTop: true)
}

It scrolls to top but not actually to top of tableView.

@andreamazz
Copy link
Owner

See #377 and #378

@arashetm
Copy link
Author

Didn't help me!
It doesn't work properly.
Actually I call the showNavbar method in the action of segmentValueChanged.

@andreamazz
Copy link
Owner

Hi @arashetm
I'm testing against the Demo project, attached the segment action like so:

  @IBAction func segmentChange() {
    if let navigationController = self.navigationController as? ScrollingNavigationController {
      navigationController.showNavbar(animated: true, duration: 0.2, scrollToTop: true)
    }
  }

and it seems to be working fine. Can you share a sample project showing the issue?

@arashetm
Copy link
Author

arashetm commented Jan 21, 2020

I figured out what's the problem!
If I want to scroll to the top without changing segment it works like charm, but if I want to update my table view data source alongside scrolling to the top it doesn't work properly.
Here is my code:

@IBAction func segmentValueChanged(_ sender: UISegmentedControl) {
    _ = self.scrollViewShouldScrollToTop(self.archiveTableView)
    self.setBannersFrame()        //setting table header view
    // change table view's data based on the selected segment
    self.reloadArchiveTableView() // reloading table view
}

I found a hacky slution for this::

@IBAction func segmentValueChanged(_ sender: UISegmentedControl) {
    _ = self.scrollViewShouldScrollToTop(self.archiveTableView)
    self.setBannersFrame()        //setting table header view
    // change table view's data based on the selected segment
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
        self.reloadArchiveTableView() // reloading table view
    }
}

Maybe you need to add completion handler for showNavbar method.

@andreamazz
Copy link
Owner

Sounds good, checkout the latest master commit for the completion handler in showNavbar

@arashetm
Copy link
Author

It didn't work ...

@andreamazz
Copy link
Owner

I'm afraid it's still triggered too early, before UIKit rearranges the table

@arashetm
Copy link
Author

Do you have any suggestions?
Reloading should start exactly right after scroll did end scrolling, could we use scrollViewDidEndScrollingAnimation?
I need to achieve this.

@andreamazz
Copy link
Owner

andreamazz commented Jan 22, 2020

Checkout the updated demo in master, the tableview now scrolls to top and updates the data source, and it seems to be working. Maybe it helps, let me know
b280a88#diff-42fa19733453bd36493bea5901519e2d

@andreamazz
Copy link
Owner

Any feedback?

@uzman
Copy link

uzman commented Dec 5, 2021

you can fix it by calling collectionview's scrolltoItem method in the completion block:

    open func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
        if let navigationController = self.navigationController as? ScrollingNavigationController {
            navigationController.showNavbar(animated: false, duration: 0.1, scrollToTop: false) {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                    self.collectionView.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
                }
            }
        }
        return true
    }

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

No branches or pull requests

3 participants