2

I have a tab bar item which is connected navigation controller with a UIViewController as the root view controller. The first touch on the tab bar item switches to that view. The second touch pops to the root view controller. The third touch does not scroll to the top.

I've seen this scroll-to-top behavior in other apps, but after searching the webs, I cannot find out anything about it.

Is this default behavior for scroll views or table views attached to tab bar items, or is it something I need to implement myself?

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117

4 Answers4

3

I realize this is an older question, but I'm also looking to create this behavior, and I think I have a simpler solution.

First, set your AppDelegate to be the delegate for your UITabBarController. Then add this method to AppDelegate.m:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if ([tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex]==viewController)
    {
        if ([viewController isKindOfClass:[UITableViewController class]])
        {
            [[(UITableViewController *)viewController tableView] setContentOffset:CGPointZero animated:YES];
        }
        else if ([viewController isKindOfClass:[UINavigationController class]])
        {
            UINavigationController *nav = (UINavigationController *)viewController;
            if ([nav.visibleViewController isKindOfClass:[UITableViewController class]])
                [[(UITableViewController *)nav.visibleViewController tableView] setContentOffset:CGPointZero animated:YES];
        }
    }

    return YES;
}

This works if your tab points at a UITableViewController or at a UINavigationController with a UITableViewController as the root view, and you don't have to worry about distinguishing between which UITableViewController is affected, sending notifications, etc.

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Sean Mahan
  • 469
  • 3
  • 13
  • Rather than `setContentOffset`, I'd suggest using `scrollToRowAtIndexPath:[NSIndexPath] indexPathForItem:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];` – Colm Doyle May 15 '15 at 11:27
2

Here is the solution to scroll to top of the table view when tab bar is clicked

In AppDelegate set tabbar delegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (tabBarController.selectedIndex == 0) {

        UINavigationController *selectedNav = [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex];
        UIViewController *currentVC = selectedNav.visibleViewController;
        if([currentVC isMemberOfClass:NSClassFromString(@"HomeViewController")])
        {

            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshView" object:nil];
        }
    }
    return YES;
}

In HomeViewController.m view did load listen for the notification

 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refreshView:)
                                                 name:@"refreshView"
                                               object:nil];

Refresh method

 -(void)refreshView:(NSNotification *) notification{
        if (self == self.navigationController.topViewController)
            [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
    }
vamsi575kg
  • 594
  • 1
  • 7
  • 23
1

No, this isn't default behaviour, you have to implement it yourself.

I'd do it by making the application delegate the delegate of the tab bar controller, and implement -tabBarController:didSelectViewController: to post a notification. Listen for that notification in your table view controller and do something like:

if (self == self.navigationController.topViewController)
    [self.tableView scrollToTop];
hatfinch
  • 3,095
  • 24
  • 35
  • I don't want to scroll to the top if tab bar item touch caused the view to switch. How can I tell if I'm switching views? Hopefully something better than `-tabBarController:shouldSelectViewController:`. – Jeffery Thomas May 31 '12 at 20:22
  • No, I think testing the current selected tab in -shouldSelect... is probably your best bet :( – hatfinch Jun 01 '12 at 09:13
  • OK, a programmer's gotta do what a programmer's gotta do. Thanks for the help. – Jeffery Thomas Jun 01 '12 at 10:37
0

Since your tab controller can only have one delegate, you may want to look at the answer to this question, which describes how to listen for the tap using KVO.

Community
  • 1
  • 1
Simon
  • 25,468
  • 44
  • 152
  • 266