jape
jape

Reputation: 2901

Scroll to top of view on second click of custom tab bar: Objective-C

I am trying to make it so when a user clicks the tab bar item a second time, they will be automatically scrolled to the top of the current screen.

I have a custom tab bar view with the following code to switch between the views:

- (IBAction)onTabSelectionChange:(id)sender {
    UIButton *btn = (UIButton*)sender;
    previousIndex = currentIndex;
    currentIndex = btn.tag;

    if (btn.tag == previousIndex){
       // This is where the code should probably go?
    } else {
        if (previousIndex != 0){
            UIButton *btnpreviousIndex = (UIButton*)[tabView viewWithTag:previousIndex];

            if (btnpreviousIndex != nil && previousIndex != -1){
                [btnpreviousIndex setSelected:NO];
            }
            [btn setSelected:YES];
        }
    }
    appDelegate.currentTab = currentIndex;

    switch (currentIndex) {
        case TABHOME: {
            UINavigationController *navController = [appDelegate.arrViewControllers objectAtIndex:0];
            navController.navigationBarHidden = YES;
            [navController popToRootViewControllerAnimated:NO];
            [self presentThisView: navController];
        }
            break;
        case TABTIMELINE: {
            UINavigationController *navController = [appDelegate.arrViewControllers objectAtIndex:1];
            [self presentThisView: navController];
        }
            break;
        case TABNOTIFICATION: {
            UINavigationController *navController = [appDelegate.arrViewControllers objectAtIndex:2];
            [self presentThisView: navController];
            break;
        }
        case TABMISCELLANEOUS: {
            UINavigationController *navController = [appDelegate.arrViewControllers objectAtIndex:3];
            [self presentThisView: navController];
            break;
        }
        default:
            break;
    }
}

Thank you in advance for the help!

Upvotes: 0

Views: 514

Answers (2)

Parth Pandya
Parth Pandya

Reputation: 1490

You can use this method to scroll to any collectionview item you want,

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath 
               atScrollPosition:(UICollectionViewScrollPosition)scrollPosition 
                       animated:(BOOL)animated

Or you can also use, [collectionView setContentOffset:CGPointZero animated:YES];

if you wish to return to the very beginning of the collection, you can simply use CGPointZero

Upvotes: 0

Joe Benton
Joe Benton

Reputation: 3753

If it's a scrollview then yes you can just put this code in the place you indicated:

[scrollView setContentOffset:CGPointZero animated:YES];

Update:

You need to access the view controller at the current tab index and then you can get the collectionView inside it and do:

[self.collectionView setContentOffset:CGPointMake(0.0f,0.0f)];

Really you should implement the UITabBarControllerDelegate and put your current code in the method

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController*)viewController {

This way you know what was the previous and selected view controllers and can then also access the collection view inside, as I suggested.

Upvotes: 1

Related Questions