iPhone: Weird space at the top of UINavigationController

I’m having a strange problem with adding a UINavigationController to my iPhone application. I add the controller as follows:

myViewController *viewController = [[myViewController alloc] initWithNibName:@"myView" bundle:nil];

myNavigationViewController *navigationController = [[myNavigationViewController alloc] initWithRootViewController:viewController];

UIView *finalView = myeNavigationViewController.view;

[self.view addSubview:finalView];

All seems to work as planned except I get a weird white space at the top of my view between the status bar and the UINavigationController title bar.
alt text http://img.ioscake.com/iphone/problem.png

  • UINavigationController alternatives
  • Pass data through navigation back button
  • ios navigation Stack Manipulation
  • iOS App Freezes on PushViewController
  • Set a custom subclass of UINavigationBar in UINavigationController programmatically
  • How to subclass UINavigationBar for a UINavigationController programmatically?
  • I’ve searched online but don’t really know what to search for. Has anyone else had this problem? Can you point me in the direction of some help?

    Thanks in advance.

    6 Solutions Collect From Internet About “iPhone: Weird space at the top of UINavigationController”

    Check out the answers in this question:

    Not sure why UIView is being nudged up by around 10px

    What does the line

    UIView *finalView = myeNavigationViewController.view;

    add to the code? It’s redundant as you can add the view directly without assigning it to a UIView first – plus it’s incorrect as it references the myNavigationController and not navigationController..
    I tend to do this

    myViewController *viewController = [[myViewController alloc] initWithNibName:@"myView" bundle:nil];    
    myNavigationViewController *navigationController = [[myNavigationViewController alloc] initWithRootViewController:viewController];
    [navigationController.view setFrame: [self.view bounds]];
    navigationController.delegate = self;
    [self.view addSubview:[navigationController view]];

    Setting the frame to the bounds also removes the white space at the top you were asking about.

    The issue is that UINavigationController ideally should be the direct subView of UIWindow. It will position and size right by itself. When you add UINavigationController into another custom view of a UIWindow subview, you need to take care of the position and size of this custom view by taking into account whether the status bar is shown or not in the UIWindow.

    My suggestion is to make the custom view as a subclass of UINavigationController:

    mySubClass_NavigationController*nav=[[mySubClass_NavigationController alloc] initWithRootViewController:viewController ];
    [myUIWindow addSubview:nav.view];

    and inside the mySubClass_NavigationController, you can do all the customization that you are doing now in your self (whatever that controller is).

    I struggled with this for a while too using very similar code to the op’s and also had a white bar above my navigation controller.

    My problem occurred when adding the UINavigationController as a view in a UITabController. The space in my case was caused by the UINavigationBar part of the UINavigationController taking into account the status bar and it was actually overlapping part of the view that I was trying to show in the UINavigationController.

    This is the code I ended up with in loadView in one of my UITabBarController view controllers.

    SomeUITableViewController *screenList = [[SomeUITableViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc]
    CGRect frame = [[navController navigationBar] frame];
    frame.origin.y = 0; // Was 20, set to 0 to not take into account the status bar.
    [[navController navigationBar] setFrame:frame];
    [self setView:[navController view]];

    There’s some more information at http://discussions.apple.com/message.jspa?messageID=7890362.

    There is an obscure property in IB called “Hides Bottom Bar on Push”. Just check it. It solved the problem for me.

    Maybe you have somehow gotten yourself two UIViews,
    each with a status bar. Check the xib.