iPhone: Weird space at the top of UINavigationController

前端 未结 6 654
轻奢々
轻奢々 2020-12-08 16:45

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

myViewController *viewControlle         


        
相关标签:
6条回答
  • 2020-12-08 17:23

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

    0 讨论(0)
  • 2020-12-08 17:25

    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).

    0 讨论(0)
  • 2020-12-08 17:29

    Check out the answers in this question:

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

    0 讨论(0)
  • 2020-12-08 17:33

    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.

    0 讨论(0)
  • 2020-12-08 17:40

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

    0 讨论(0)
  • 2020-12-08 17:43

    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]
                                             initWithRootViewController:screenList];
    
    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.

    0 讨论(0)
提交回复
热议问题