I make a UINavigationController
a child of another view controller through containment. Everything works fine, except for a strange issue that occurs when launching the app with the in-call status bar turned ON and then switching it back OFF after the app UI is on the screen. There appears a strange black gap in place of the status bar.
Consider the self-contained example app 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 is:
The UI should now look as follows:
Anyone know how to fix the problem?
You are adding a view (the navigation controller's view) as subview to another view (the parent view controller's 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's view has a frame, and maintains it in relation to its superview.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With