Gap appears between navigation bar and view after rotating & tab switching

Posted by Bogatyr on Stack Overflow See other posts from Stack Overflow or by Bogatyr
Published on 2010-05-17T06:28:25Z Indexed on 2010/05/17 6:30 UTC
Read the original article Hit count: 355

Filed under:

My iphone application is showing strange behavior when rotating: a gap appears between the navigation title and content view inside a tab bar view (details on how to reproduce are below). I've created a tiny test case that exhibits the same problem: a custom root UIViewController, which creates and displays a UITabBarController programmatically, which has two tabs: 1) plain UIViewController, and 2) UINavigationController created programmatically with a single plain UIViewController content view.

The complete code for the application is in the root controller's viewDidLoad (every "*VC" class is a totally vanilla UIViewController subclass with XIB for user interface from XCode, with only the view background color changed to clearly identify each view, nothing else).

Here's the viewDidLoad code, and the shouldAutorotateToInterfaceOrientation code, this code is the entire application basically:

- (void)viewDidLoad {
    [super viewDidLoad];

    FirstVC *fvc = [[FirstVC alloc] initWithNibName:@"FirstVC" bundle:nil];
    NavContentsVC *ncvc = [[NavContentsVC alloc] initWithNibName:@"NavContentsVC" bundle:nil];
    UINavigationController *svc = [[UINavigationController alloc] initWithRootViewController:ncvc];

    NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
    [localControllersArray addObject:fvc];
    [localControllersArray addObject:svc];

    fvc.title = @"FirstVC-Title";
    ncvc.title = @"NavContents-Title";

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.view.frame = CGRectMake(0, 0, 320, 460);
    [tbc setViewControllers:localControllersArray];
    [self.view addSubview:tbc.view];

    [localControllersArray release];
    [ncvc release];
    [svc release];
    [fvc release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    return YES;
}

Here's how to reproduce the problem:

1) start application

2) rotate device (happens in simulator, too) to landscape (UITabBar properly rotates)

3) click on tab 2

4) rotate device to portrait -- notice gap of root view controller's background color of about 10 pixels high beneath the Navigation title bar and the Navigation content view.

5) click tab 1

6) click tab 2

And the gap is gone! From my real application, I see that the gap remains during all VC push and pops while the NavigationController tab is active. Switching away to a different tab and back to the Nav tab clears up the gap.

What am I doing wrong? I'm running on SDK 3.1.3, this happens both on the simulator and on the device. Except for this particular sequence, everything seems to work fine. Help!

© Stack Overflow or respective owner

Related posts about iphone