Search code examples
iosuiimageviewaddsubviewappdelegate

Cant Add UIImageView to self.window in didFinishLaunchingWithOptions


I am attempting to add an UIImageView as the first view that is shown after my Default.png disappears. This should be super easy but for some reason I am getting a black screen.

// AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

UIImageView *newimageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[newimageview setImage:[UIImage imageNamed:@"myimage.png"]];


[_window addSubview:newimageview];
[_window bringSubviewToFront:newimageview]; 

}

I also tried setting the background color of newimageview to see if I just wasn't loading my image file properly, but the screen still appeared black.

Any help would be appreciated, I feel pretty dumb at the moment.

I just created a new project and modified only the didFinishLaunchingWithOptions method. Here is exactly the code I am using. I changed nothing else in the project:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UIImageView *newimageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-basketball"]];

    [self.window addSubview:newimageview];
    [self.window bringSubviewToFront:newimageview];

    // Override point for customization after application launch.
    //self.window.backgroundColor = [UIColor whiteColor];
    //[self.window makeKeyAndVisible];
    return YES;
}

And i am still getting the same black screen result.

I have added bg-basketball.png and bg-basketball@2x.png to my project and can see both images in XCode.


Solution

  • self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];//Add this line
    

    This is works for me.