UINavigationController within ViewController, gap at top of view

后端 未结 11 994
不知归路
不知归路 2021-02-04 03:24

I\'m working on a universal app, and I\'m trying to share as much code as possible between the iPhone and iPad versions. I need to use a TabBarController as my root view contro

相关标签:
11条回答
  • 2021-02-04 03:27

    had a similar problem with the unwanted gap between the status bar and the view directly below it, solved it by going to autosizing in interface builder and setting the view in question to stick to the top of its parent view.

    0 讨论(0)
  • 2021-02-04 03:33

    UINavigationController is normally displayed as a full-screen controller, which means that (when displayed as the root view controller of the window) the top part of its view is placed under the status bar. It then manually positions its navigation bar and content view to not underlap the status bar when the status bar is visible. Unfortunately, it doesn't really handle things correctly when it is being positioned by some other view controller, it (sometimes) assumes it needs to leave that 20px gap without checking whether its view actually is under the status bar.

    The solution is to set wantsFullScreenLayout on the UINavigationController to NO, so it won't even attempt to leave that gap for the status bar.

    0 讨论(0)
  • 2021-02-04 03:35

    To fix this problem just check the box Wants Full Screen on the storyboard.

    The problem appears because the ParentViewController is showing the navigation bar.

    As apple documentation said :

    If your app displays the status bar, the view shrinks so that it does not underlap the status bar. After all, if the status bar is opaque, there is no way to see or interact with the content lying underneath it. However, if your app displays a translucent status bar, you can set the value of your view controller’s wantsFullScreenLayout property to YES to allow your view to be displayed full screen. The status bar is drawn over the top of the view.

    0 讨论(0)
  • 2021-02-04 03:37

    Oddly enough, what's helping for me in iOS 6 is subclassing UINavigationController and implementing this method:

    - (BOOL)wantsFullScreenLayout {
        return NO;
    }
    

    Unchecking "Wants Full Screen" in my storyboard didn't do the trick, so I don't know what's different. This could certainly break in a future version of iOS. But for now, I'll take it.

    0 讨论(0)
  • 2021-02-04 03:38

    I was able to brute force as follows. In viewDidLoad or loadView:

    if (self.navigationController.navigationBar.frame.origin.y > 0.0) {
        self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0);
    }
    

    And in viewDidAppear:

    if (self.view.superview.frame.origin.y > 44.0) {
        UIView *container = self.view.superview;
        container.frame = CGRectMake(container.frame.origin.x, container.frame.origin.y - 20.0, container.frame.size.width, container.frame.size.height + 20.0);
    }
    

    It's ugly, but it seems to work.

    0 讨论(0)
  • 2021-02-04 03:40

    I solved this in my app by hiding then showing the navigation bar after adding the navigation controllers view. eg.

    [parentView addSubview:navController.view];
    [navController setNavigationBarHidden:YES];
    [navController setNavigationBarHidden:NO];
    
    0 讨论(0)
提交回复
热议问题