Search code examples
objective-ciosios6

Gap in UITabBarController inside UINavigationController


I have a root view of UINavigationController containing a UITabBarController. The tab bar controller has two simple views. When it first launches either in the iOS 6 simulator or on a device, a gap appears above the first view. Switching to the second tab and back causes the gap to disappear. This only started happening since iOS 6. iOS 5 works just fine. Any ideas what changed?

Screenshot:

Screenshot

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    UIView *dummyView1 = [[UIView alloc] init];
    [dummyView1 setBackgroundColor:[UIColor redColor]];
    UIViewController *dummyViewController1 = [[UIViewController alloc] init];
    [dummyViewController1 setView:dummyView1];

    UIView *dummyView2 = [[UIView alloc] init];
    [dummyView2 setBackgroundColor:[UIColor blueColor]];
    UIViewController *dummyViewController2 = [[UIViewController alloc] init];
    [dummyViewController2 setView:dummyView2];

    NSArray *viewControllers = [NSArray arrayWithObjects:dummyViewController1, dummyViewController2, nil];

    [tabBarController setViewControllers:viewControllers];

    UINavigationController *rootNavController = [[UINavigationController alloc] initWithRootViewController:tabBarController];
    [[self window] setRootViewController:rootNavController];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

Solution

  • Apple explicitly tells us in I believe the Application Programing Guide (or maybe the ViewController Guide) that a tabBarController should NOT be placed in a navigation controller. You need to reverse the order. You can play tricks with the tabBarController by switching its array of controllers, and having one set hide the tabBar. But any attempt to use the order as you have now is doomed to fail at some point - you may make it work in say iOS5.1, then see it break in 6, 6.1, or 7.