开发者

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

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!


This problem occurs when you nest a UINavigationController within another UIViewController (in this case a UITabBarController). If you had the UINavigationController as the root view controller, then this problem wouldn't occur.

One solution may be to go in and alter the frame of the navigation bar (set the y origin from 0 to 20), but the documentation states explicitly not to do this. So to me, this is an indication that it isn't considered good UI to nest a UINavigationController - you shouldn't be doing it.

Please let me know what you think - thanks. :)


A workaround works in some occasion:

After rotating, force a refresh of the NavigationBar and therefore the frame of its view is resized properly. Some code like this:

  • (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

{

// if _navigationController is showing
[_navigationController setNavigationBarHidden:YES];

[_navigationController setNavigationBarHidden:NO];

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜