Eyal
Eyal

Reputation: 10818

Scroll to top when tapping the status bar

I got a view controller (lets call it MainViewContoller) that's present 3 different tables (one in a time), user can tap a segment control to switch between those tables.

To present those 3 tables, MainViewContoller has 3 other view controllers (A, B and C), each has a UITableView as a subview and handle it's own data.
When a MainViewContoller is loaded, it initiate controllers A, B and C, and add their tableViews to it's view:

- (void)viewDidLoad {
    [super viewDidLoad];

    ViewControllerA *vcA = [ViewControllerA alloc] init];
    [self.view addSubview:vcA.view];

    ViewControllerB *vcB = [ViewControllerB alloc] init];
    [self.view addSubview:vcB.view];

    ViewControllerC *vcC = [ViewControllerC alloc] init];
    [self.view addSubview:vcC.view];
}

So for example when user tap the segment control and choose A, the MainViewContoller hide tables B and C, and unhide table A. Something like this:

if (userTapOnA) {
    self.viewControllerA.tableView.hidden = NO;
    self.viewControllerB.tableView.hidden = YES;
    self.viewControllerC.tableView.hidden = YES;
}  

The problem:

When user tap the status bar I want that the current visible table will scroll to top.
This behavior is pretty basic and one gets it for free when using a regular view controller, but as you can see my view controller is not regular.
I suppose that by using other controllers view as MainViewContoller view I break the default behavior, so my MainViewContoller doesn't handle the status bar tap.

Someone got an idea how to solve that?

Upvotes: 5

Views: 7182

Answers (2)

carlos
carlos

Reputation: 2624

This is directly from the UIScrollView header file:

/* When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its scrollsToTop property is YES, its delegate does not return NO from shouldScrollViewScrollToTop, and it is not already at the top. On iPhone, we execute this gesture only if there's one on-screen scroll view with scrollsToTop == YES. If more than one is found, none will be scrolled. */

@property(nonatomic) BOOL scrollsToTop; // default is YES.

So in your case, set all scrollsToTop to NO, except the one you want to enable at that particular moment.

Upvotes: 26

Adam
Adam

Reputation: 26917

You should register your nested controllers as child controllers.

[self addChildViewController:vcA];
[self addChildViewController:vcB];
[self addChildViewController:vcC];

I'm not sure if this will help to solve your issue, but that's the right way to do it.

Upvotes: 2

Related Questions