James Hao
James Hao

Reputation: 893

Single view application has a black background which cannot be changed

I am using objective-c to develop a single view app which is targeted for 11.4 iOS, and the Xcode version is 9.4.1.

After creation, there are Main.storyboard and LaunchScreen.storyboard, I changed the background for Main.storyboard to yellow.

Then load rootViewController with blow codesnip:

self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];

When I run the app, the app's background is black instead of yellow, what is wrong?

======================== add screenshot: enter image description here

======================== add code for init rootviewcontroller:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen alloc] bounds]];

    self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

======================= I can confirm it is that code in didFinishLaunchingWithOptions failed, all work fine if I comment out those code in the method, don't know why.

Upvotes: 1

Views: 497

Answers (5)

Sargis
Sargis

Reputation: 1334

Use this code.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

Upvotes: 1

James Hao
James Hao

Reputation: 893

found the cause, the

initWithFrame:[[UIScreen alloc] bounds] 

should be

initWithFrame:[[UIScreen mainScreen] bounds]

Upvotes: 1

SThakur
SThakur

Reputation: 282

You have a “Main.storyboard” already added in the project. Select its ViewController , go to property inspector . tick the “initial view controller” option. Create View controller in the “Main.storyboard” say LoginVC , give it the Class and Storyboard ID

Create rootviewcontroller in app delegate.enter image description here enter image description here

enter image description here

let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "LoginVC") as UIViewController
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = initialViewControlleripad
        self.window?.makeKeyAndVisible()

Upvotes: 0

Subhajit Halder
Subhajit Halder

Reputation: 1423

Your do not need to fire any code to get your Main.storyboard initial view controller.

Delete the code and go to Project settings -> General tab here you will find Main Interface see if it is set to Main(your storyboard name)

enter image description here

Also check your in you storyboard that the entry point is set to your desired viewcontroller or not.

enter image description here

Run your project and see.

Upvotes: 2

Vinay Patel
Vinay Patel

Reputation: 96

Use below code to get your view controller on start up,

self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"yourViewController Storyboard ID."];

Upvotes: 1

Related Questions