UINavigationController within ViewController, gap at top of view

后端 未结 11 1026
不知归路
不知归路 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:41

    Not sure whether it'll be of any help, but I've had similar issues in the past and have been able to resolve this by setting the frame of the UINavigationController view to CGRectZero when adding it to its superview.

    In layoutSubviews I update the frame of the UINavigationController view of course.

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

    The proper answer is:

    CGRect r = [[_navController view] frame];
    r.origin = CGPointMake(0.0f, -20.0f);
    [[_navController view] setFrame:r];
    
    0 讨论(0)
  • 2021-02-04 03:48

    I ran into the same issue when adding a UITableView to my ViewController on viewDidLoad. Instead of grabbing the frame from self.view, I got it form the main window so that it looks like this

    UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
    self.uiTable = [[UITableView alloc] initWithFrame:[keyWindow frame] style:UITableViewStylePlain];
    
    0 讨论(0)
  • 2021-02-04 03:50

    In my view that is displayed inside the navigationcontroller I put this code inside the viewDidAppear

    This code may not be perfect for every application but it fixed my issue I spent several hours kicking at..

        // START OF BUG FIX FOR iOS
    if (self.navigationController.navigationBar.frame.origin.y ==20) {
        // Move the navigation Bar up
        [self.navigationController.navigationBar setFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
        // move up the table view
         [self.view setFrame:CGRectMake(0, -20, self.view.frame.size.width, self.view.frame.size.height+20)];
    }
    // END OF BUG FIX for IOS6
    
    0 讨论(0)
  • 2021-02-04 03:52

    This is a common issue when adding view controllers to a tab bar.

    From Apple's documentation (Tab Bar Controllers - Tab Bars and Full-Screen Layout):

    Tab bar controllers support full-screen layout differently from the way most other controllers support it. You can still set the wantsFullScreenLayout property of your custom view controller to YES if you want its view to underlap the status bar or a navigation bar (if present). However, setting this property to YES does not cause the view to underlap the tab bar view. The tab bar controller always resizes your view to prevent it from underlapping the tab bar.

    In words of code, you should do the following:

    UINavigationController *myNavController = [[UINavigationView alloc] init];
    myNavController.wantsFullScreenLayout = YES;
    
    //...
    
    NSArray* controllers = [NSArray arrayWithObjects:myNavController, nil];
    myTabBarController.viewControllers = controllers;
    

    If, however, you run into the problem that when opening application in orientation other than UIInterfaceOrientationPortrait your myNavController's view moves 20 pix off the screen to the top, then you will have set controller's wantsFullScreenLayout property dynamically (instead of the above solution), depending on the initial orientation. I do it using a static variable defined in your navigation controller implementation:

    static UIInterfaceOrientation _initialOrientation = -1;
    

    After that you need to overload the viewDidAppear: method and set the variable appropriately:

    - (void)viewDidAppear:(BOOL)animated
    {
      if (_initialOrientation == -1)
        _initialOrientation = [[UIApplication sharedApplication] statusBarOrientation];
      self.wantsFullScreenLayout = (_initialOrientation != UIInterfaceOrientationPortrait);
    
      [super viewDidAppear:animated];
    }
    

    Hope this helps.

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