Sebastian Sebastian - 5 months ago 11
iOS Question

iOS Simulator shows either blank black or white screen

I am running one project with no problems at all, but the second won't show up in iOS Simulator. What it does show depends on what's in my

didFinishLaunchingWithOptions
method in
AppDelegate.m
.

Gives black screen:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}


Gives white screen:

- (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];
return YES;
}


Any ideas for a fix?

Answer

If you use .xib:

In first case you see black screen because you even not created window, in the second - you created white window, but without root controller.

You need to specify rootViewController of the window:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    // Your don't need specify window color if you add root controller, you will not see window because root controller will be above window.
    //self.window.backgroundColor = [UIColor whiteColor];

    self.window.rootViewController = [YourViewController new];

    [self.window makeKeyAndVisible];
    return YES;
}

If you use storyboard leave the first example:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}

And make sure that in proj file you specified the main storyboard: enter image description here

Also make sure that you set one of the view controllers in the storyboard as an initial: enter image description here

Comments