0

I have a custom pop transition and it depends on the UITableView being scrolled to the top before performing the transition. The second I tap the back button of my UIViewController I want to scroll the UITableView up which will then have the correct state for my transition.

I already tried to scroll the UITableView up before the view will get dismissed:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
}

This however failed to scroll at all. How can I move the UITableView to the top before the UIViewController gets dismissed?

Hapeki
  • 2,153
  • 1
  • 20
  • 36
  • I run with this code and it works just fine? Please include more of your code including the transition you are trying to attempt – rolling_codes Jan 17 '20 at 17:20
  • 2
    `viewWillDisappear()` is too late to try and animate / update the UI. Whatever you are doing to start the *"custom pop transition"* (button tap?) is where you need to scroll the table view, and *then* begin your pop transition. – DonMag Jan 17 '20 at 17:36
  • @DonMag I indeed feel that's the case, the problem is that the button involved is the `<` back button of the navigation controller. It's a system button handling the dismiss I don't do this myself. I'll investigate if I can hook into this event when the button is tapped. – Hapeki Jan 17 '20 at 17:44
  • 1
    @Hapeki - you can Hide the default Back button and put your own button there... or, rethink your transition approach. Without seeing it, it's not clear *why* you need the tableView scrolled to the top (seems like it might be confusing to the user anyway?). – DonMag Jan 17 '20 at 17:48
  • 1
    I recommend _not_ hiding the default Back button and adding your own, because that also breaks the swipe-left-to-navigate-back behavior that users expect. I agree with you about the scrolling being a confusing use case, though. – NRitH Jan 17 '20 at 17:59
  • @NRitH I can understand that scrolling to the top sounds weird without seeing it firsthand. An example of this use case can be found in the Wallet app of iOS where you tap a card and see the details below it. Scroll all the way down and then dismiss the view - you'll see that they first scroll the table view all the way to the top before performing the pop transition. Anyways, I got the scroll working by adding my custom leftBarButton item but as you pointed out it's breaking the swipe-left-to-navigate behaviour. Thank you guys ! – Hapeki Jan 17 '20 at 18:08
  • 1
    @Hapeki - to take your **Wallet** app example... selecting a card does not ***push*** to a new view controller, and tapping `Done` does not ***pop*** back in a navigation controller stack. So, the animation does not confuse the navigation. – DonMag Jan 17 '20 at 20:56
  • @DonMag indeed it is a push, due to certain constraints from the project im not allowed to present it, this is an ugly bypass to solve the issue, but i'll rethink this. Thanks for your feedback :) – Hapeki Jan 20 '20 at 10:32

1 Answers1

1

Overriding willMove(toParent:) and checking for parent == nil should allow you to perform the animation at the required time. See this response on intercepting nav back button.

okat
  • 71
  • 1
  • 3