Status bar incorrectly set using the UINavigationController

I make a UINavigationController child of another view controller through containment. Everything works fine, except for the strange problem that occurs when you turn on the application with the status bar in a call state , and then turn it off after the user interface of the application is on the screen. A strange black gap appears instead of the status bar.

Consider the attached sample application below:

 #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)opt { // Content UILabel * aLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 200, 40)]; aLbl.text = @"In-call status bar issue"; UIViewController * aContent = [[UIViewController alloc] init]; aContent.title = @"Title"; aContent.view.backgroundColor = UIColor.whiteColor; [aContent.view addSubview:aLbl]; UINavigationController * aNav = [[UINavigationController alloc] initWithRootViewController:aContent]; // Parentmost view controller containing the navigation view controller UIViewController * aParent = [[UIViewController alloc] init]; [aParent.view addSubview:aNav.view]; [aParent addChildViewController:aNav]; [aNav didMoveToParentViewController:aParent]; self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; self.window.rootViewController = aParent; [self.window makeKeyAndVisible]; return YES; } @end int main(int argc, char ** argv) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, @"AppDelegate"); } } 

The easiest way to reproduce the problem:

  • Launch iOS Simulator.
  • Press โŒ˜ + Y to turn on the call status bar. The status bar will turn wide and green.
  • Launch the application manually and wait for the navigation controller to appear.
  • Press โŒ˜ + Y again to turn off the call status bar.

The user interface should now look like this: Black gap instead of the status bar when switching the in-call status bar back off

Does anyone know how to fix the problem?

+7
ios uiviewcontroller statusbar uinavigationcontroller
source share
1 answer

You add a view (view of the navigation controller) as a subview to another view (view of the controller of the parent view) without giving it a frame! This is something you should never do.

Just add these lines to your code:

 aNav.view.frame = aParent.view.bounds; aNav.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 

Now the navigation controller view has a frame and supports it in relation to its supervision.

+6
source share

All Articles