Tap tab bar to scroll to top of UITableViewController

后端 未结 12 1087
面向向阳花
面向向阳花 2020-12-23 10:15

Tapping the tab bar icon for the current navigation controller already returns the user to the root view, but if they are scrolled way down, if they tap it again I want it t

12条回答
  •  青春惊慌失措
    2020-12-23 10:26

    Implement the UITabBarControllerDelegate method tabBarController:didSelectViewController: to be notified when the user selects a tab. This method is also called when the same tab button is tapped again, even if that tab is already selected.

    A good place to implement this delegate would probably be your AppDelegate. Or the object that logically "owns" the tab bar controller.

    I would declare and implement a method that can be called on your view controllers to scroll the UICollectionView.

    - (void)tabBarController:(UITabBarController *)tabBarController 
     didSelectViewController:(UIViewController *)viewController
    {
        static UIViewController *previousController = nil;
        if (previousController == viewController) {
            // the same tab was tapped a second time
            if ([viewController respondsToSelector:@selector(scrollToTop)]) {
                [viewController scrollToTop];
            }
        }
        previousController = viewController;
    }
    

提交回复
热议问题