Consider the following app delegate entry point:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// Custom view controller
main = [[MainView alloc] initWithNibName:@"MainView" bundle:nil];
[self.window addSubview:main.view];
return YES;
}
Everything is Apple stock code, except the “custom view controller” part. There, a custom view controller is grabbed, and effectively added as the main view of the app.
Problem: the widgets in the view are 20px above where they should be. In other words, top 20px of the view’s contents are obscured by the status bar.
But: after rotation, the view and its contents are positioned correctly, and the status bar overlap no longer appears.
What’s going on here? And specifically, what’s the pattern/best practice to make sure that things are positioned correctly also when adding the subview to the window initially?
You are using the wrong method for initializing your UIWindow’s frame. Yous should be using
[[UIScreen mainScreen] applicationFrame]for exactly the reason you are talking about. Read this for more (basicallyapplicationFrametakes care of the status bar issue).