Jonas Geiregat
Jonas Geiregat

Reputation: 5442

Adding splashscreen to iphone app in AppDelegate

I'm running xcode-4.2 and the project is based for ios5 using storyboards.

I've created a single view application , using the template provided by Apple. In the storyboard I the removed the viewcontroller created for me and added a UITabBarController. Next I added a new class MyTabBarController which is a subclass of UITabBarController.

Now I want to show a splashscreen before the TabBar appears. So I can do some loading and calculation in the background.

I thought AppDelegate.m would be a good place for this. Since that's the place where my rootview get's loaded not ? Or should a show the splashscreen from the rootviewcontroller which is MyTabBarController in my case ?

So I created a xib file. I'm surprised you can add .xib files to ios5 storyboard projects. The xib file is called SplashView.xib it has a single view with an image on it.

Code in AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_splashScreen = [[UIViewController alloc] initWithNibName:@"SplashView" bundle:nil];
//_splashScreen is defined as:@property (strong, nonatomic) UIViewController *splashScreen;

[_window.rootViewController presentModalViewController:_splashScreen animated:NO];

[self performSelector:@selector(hideSplash) withObject:nil afterDelay:2];
return YES;

}

The problem is nothing happens. Even if I change the value from 2 to 200. The application starts up as if there is no splashscreen.

As you might have noticed I'm still struggling with the design of objective-c and iphone application. I hope a decent answer to my question will bring some clarity to the subject.

Thanks in advance!

Upvotes: 1

Views: 10075

Answers (4)

Peter Todd
Peter Todd

Reputation: 8651

I just add an identical image to the launch image to my first view controller and then fade it (or whatever animation you require) - this avoids pausing the app load in the AppDelegate. You need to ensure that the image has the same size and origin as your launch image e.g. to set the image to display on my first view controller which is a tableViewController:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.tableView.bounds];
imageView.image = [UIImage imageNamed:@"[imagename]"];
[self.tableView addSubview:imageView];
[self.tableView bringSubviewToFront:imageView];
// Fade the image
 [self fadeView:imageView];

-(void)fadeView:(UIView*)viewToFade
{
    [UIView animateWithDuration:FADE_DURATION
                     animations:^ {
                         viewToFade.alpha = 0.0;
                    }
     ];

}

Upvotes: 0

sunkehappy
sunkehappy

Reputation: 9101

If you are using storyboard, you can just add the splash UIImageView to your window.rootViewController.view like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIImage *splashImage = [UIImage autoAdjustImageNamed:@"Default.png"];
    UIImageView *splashImageView = [[UIImageView alloc] initWithImage:splashImage];
    [self.window.rootViewController.view addSubview:splashImageView];
    [self.window.rootViewController.view bringSubviewToFront:splashImageView];
    [UIView animateWithDuration:1.5f
                          delay:2.0f
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         splashImageView.alpha = .0f;
                         CGFloat x = -60.0f;
                         CGFloat y = -120.0f;
                         splashImageView.frame = CGRectMake(x,
                                                            y,
                                                            splashImageView.frame.size.width-2*x,
                                                            splashImageView.frame.size.height-2*y);
                     } completion:^(BOOL finished){
                         if (finished) {
                             [splashImageView removeFromSuperview];
                         }
                     }];

    return YES;
}

I think the reason why directly just add the UIImageView to window is because iOS will bring the rootViewController.view to front when the default splash will hide. And this will overlap the animation. This means the animation does happen but it's behind the rootViewController.

Upvotes: 2

petershine
petershine

Reputation: 3200

I've dealt with a few clients who wanted to use an animated splash. Though I'm totally against this, following Apple's HIG, those clients just don't understand...

Anyway, since - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; has to return boolean, it's very important not to halt it for anything. Also, since launch time is measured by iOS, if it's taking too long, the app will be terminated by iOS!

For this reason, I often use - (void)applicationDidBecomeActive:(UIApplication *)application; with some kind of flag to indicate if it happened at launch or at returning from background mode.

Or, you should use a NSTimer or - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay; so, didFinishLaunchingWithOptions can return without being blocked for processing your animated splash.

This delayed performSelector should be implemented not only for hiding action (like the way you intended it), but also for starting the animation.

Upvotes: 2

Randall
Randall

Reputation: 14849

Splash screens are built into iOS apps. All you need to do is create a file called Default.png and [email protected] (for retina displays) and it will work as a splash screen for when the app launches.

You can also set what these images will be in your apps info.plist.

Upvotes: 8

Related Questions