Tap tab bar to scroll to top of UITableViewController Tap tab bar to scroll to top of UITableViewController ios ios

Tap tab bar to scroll to top of UITableViewController


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;}


SWIFT 3

Here goes..

First implement the UITabBarControllerDelegate in the class and make sure the delegate is set in viewDidLoad

class DesignStoryStreamVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITabBarControllerDelegate { @IBOutlet weak var collectionView: UICollectionView! override func viewDidLoad() {        super.viewDidLoad()        self.tabBarController?.delegate = self        collectionView.delegate = self        collectionView.dataSource = self    }}

Next, put this delegate function somewhere in your class.

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {    let tabBarIndex = tabBarController.selectedIndex    print(tabBarIndex)    if tabBarIndex == 0 {        self.collectionView.setContentOffset(CGPoint.zero, animated: true)    }}

Make sure to select the correct index in the "if" statement. I included the print function so you can double check.


You can use shouldSelect rather than didSelect, which would omit the need for an external variable to keep track of the previous view controller.

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{    if ([viewController isEqual:self] && [tabBarController.selectedViewController isEqual:viewController]) {        // Do custom stuff here    }    return YES;}


matomo