Break appears between nav and view after rotating and switching tabs
My iphone app exhibits strange behavior when rotating: there is a space between the navigation title and the content in the tab bar window (details on how to play below). I created a tiny test case that shows the same problem: a custom root UIViewController that programmatically creates and displays a UITabBarController that has two tabs: 1) a simple UIViewController and 2) a UINavigationController created programmatically with one simple view of the UIViewController content.
The complete application code is in the root viewDidLoad controller (each "* VC" class is a completely vanilla subclass of the UIViewController with an XIB for the UI from Xcode, with only the view background color changing to clearly identify each view, nothing else).
Here's the viewDidLoad code and the toAutorotateToInterfaceOrientation code, this code is a whole 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) run the application
2) turn the device (also in the simulator) to the landscape (UITabBar rotates correctly)
3) click on tab 2
4) rotate the device in portrait - notice the difference in the root controller icon with a color of about 10px below the navigation title bar and the navigation content view.
5) click tab 1
6) click tab 2
And the abyss was gone! From my real application, I can see that the break remains during all VC clicks and exits while the NavigationController tab is active. Switching to another tab and back to the "Nav" tab removes the gap.
What am I doing wrong? I am working on SDK 3.1.3, this happens on both simulator and device. Except for this particular sequence, everything works fine. Help!
a source to share
This problem occurs if you embed the UINavigationController in another UIViewController (in this case, UITabBarController). If you had a UINavigationController as your root view controller, this issue would not have occurred.
One solution might be to go in and change the frame of the navbar (set the origin to 0 to 20), but the documentation clearly says not for that. So, to me, this is indicative of what is not considered a good UI for the UINavigationController socket - you shouldn't.
Please let me know what you think - thanks. :)
a source to share
The workaround works in some way:
Once rotated, force the navbar to refresh and hence its view frame will be resized correctly. Some code looks like this:
- (canceled) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
{
// if _navigationController is showing
[_navigationController setNavigationBarHidden:YES];
[_navigationController setNavigationBarHidden:NO];
}
a source to share