Adding splashscreen to iphone app in AppDelegate - objective-c

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!

Splash screens are built into iOS apps. All you need to do is create a file called Default.png and Default#2x.png (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.

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.

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.

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;
}
];
}

Related

Unable to detect iPhone Retina 4-inch screen size in simulator

I want to make my iOS application support iPhone 5. So I created a separate xib set for iPhone 5 size. Then I load each xib by checking the screen height.
This is the splash screen loading code inside the AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1;
if ([UIScreen mainScreen].bounds.size.height==480) {
viewController1 = [[SplashScreen alloc] initWithNibName:#"SplashScreen" bundle:nil];
}
if ([UIScreen mainScreen].bounds.size.height==568) {
viewController1 = [[SplashScreen alloc] initWithNibName:#"SplashScreen5" bundle:nil];
}
self.window.rootViewController = viewController1;
[self.window makeKeyAndVisible];
return YES;
}
But when I change the simulator into Retina 4-inch, my code doesn't get the emulator size. It always executes the 480 if condition.
But other apps I created like this are working properly.
What is the reason for this?
I'm having the exact same problem right now (at the worst moment, of course....).
It did work properly for several weeks, and for a unknown reason, the simulator suddenly considers the 4in simulated device as a 3.5in screen.
cleaning, reset, reboot : same situation...
EDIT : ok, problem solved. T'was because of a missing Default image in the -568#2x format. I knew that was a condition to make the system work, but xcode had apparently decided to get rid off the one I chose. oh well...

Navigation Bar on top when rotating

I have UIViewController (for example, loginVC) and I'm trying to add it's view on top of all views.
I tried to add this view to AppDelegate
[[AppDelegate sharedDelegate].window addSubview:loginVC.view];
But in this case autorotation doesn't work, so I tried to add this view to NavigationController's view. NavigationController is rootViewController:
[[AppDelegate sharedDelegate].navigationController.view addSubview:loginVC.view];
It looks good and autorotating, but it has strange behavior when rotating.
After beginning of rotation, navigation bar is showing on top of loginVC.view and at the end of rotation is going behind this view, like it shown on screenshots (I've set red background to make it more visible, background is transparent, to see all stuff behind this view):
What I've tried:
I found this somewhere on stackoverflow: disable UIView animations before rotating and enable them after rotating - doesn't look good, because rotating occurs without animation (it's a bit obvious)
tried to make navigationBar hidden before rotation and make it visible after rotation, but in this case navigationBar bringing on top of loginVC.view
Next thing I gonna do - add this view on AppDelegate's window and handle rotation manually, but maybe there is some better way to do this?
UPD:
screenshots:
You can see issue on second screenshot: navigation bar is on top
add your viewController in uinavigationcontroller and push uinavigationcontroller then always navigation bar is visible.
My friend helped me with this problem
Here is the solution:
In AppDelegate I've created UIWindow property:
//AppDelegate.h
#property (nonatomic, strong) UIWindow *loginWindow;
Initialized it when application starts
//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
....
self.loginWindow = [[UIWindow alloc] init];
self.loginWindow.windowLevel = UIWindowLevelStatusBar;
self.loginWindow.frame = [[UIScreen mainScreen] bounds];
self.loginWindow.backgroundColor = [UIColor clearColor];
....
return YES;
}
And then, in loginVC:
#interface loginVC ()
#property (nonatomic, weak) UIWindow *loginWindow;
#end
#implementation
....
- (void)show {
// setting up loginVC view
if (!self.loginWindow) {
self.loginWindow = [[AppDelegate sharedDelegate] loginWindow];
}
if (![self.loginWindow.rootViewController isEqual:self]) {
[self.loginWindow setRootViewController:self];
}
self.loginWindow.hidden = NO;
//UPD:
//[self.loginWindow makeKeyAndVisible];
//UPD2:
[self.loginWindow makeKeyWindow];
}
- (void)hide {
// hiding view and stuff
[[[AppDelegate sharedDelegate] loginWindow] setHidden:YES];
//UPD:
//[[[AppDelegate sharedDelegate] window] makeKeyAndVisible];
//UPD2:
[[[AppDelegate sharedDelegate] window] makeKeyWindow];
}
#end
UPD:
No need to use makeKeyAndVisible method of UIWindow, second window will be always on top of first one.
UPD2:
Again updating my answer, maybe it will be useful for somebody.
Without makeKeyAndVisible I couldn't use UITestFields so I uncommented that code and faced another problem:
I have UIViewController, create an instance of another UIViewController inside this controller and call [self presentViewController:...]. In presented UIViewController I'm creating loginVC, but when I call
[[[AppDelegate sharedDelegate] window] makeKeyAndVisible];
presented viewController disappears, but first view controller still has this controller as presentedViewController, so I can't present other view controllers.
My solution was change makeKeyAndVisible on makeKeyWindow.

UIPageViewControllerSpineLocation Delegate Method Not Firing

Major head-scratcher all day on this one :-(
I have an instance of a UIPageViewController that does not appear to be firing the delegate method:
-(UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
I have tried various methods of displaying the UIPageViewController and have settled on a programatic approach (as opposed to a Storyboard one) that appears to be working correctly, with one exception... when rotating the iPad to landscape the spine does not appear mid-point as expected. I simply cannot find out why the delegate method does not get called.
Code Explanation (simplified for example)
Consider three classes as follows:
RootViewController - loaded when the app starts
PageViewController - loaded by RootViewController upon user initiation
PageContentViewController - loaded by PageViewController when pages are needed
Fairly self-explanatory. The RootViewController is loaded by the app upon launch. When the user taps an image within this view controller's view (think magazine cover opening a magazine) it launches the PageViewController as follows:
PageViewController *pvc = [[PageViewController alloc] initWithNibName:#"PageView"
bundle:[NSBundle mainBundle]];
pvc.view.frame = self.view.bounds;
[self.view addSubview:pvc.view];
In the actual app there is animation etc to make the transition all nice, but essentially the PageViewController's view is loaded and takes fullscreen.
PageViewController
This is the workhorse (only relevant methods shown). I have tried various examples from the infinite world of Google and written directly from the Apple docs...
#interface PageViewController : UIViewController <UIPageViewControllerDelegate, UIPageViewControllerDataSource>
#property (nonatomic, strong) UIPageViewController *pageViewController;
#property (nonatomic, strong) NSMutableArray *modelArray;
#end
#implementation TXCategoryController
-(void)viewDidLoad
{
[super viewDidLoad];
// Simple model for demo
self.modelArray = [NSMutableArray alloc] init];
for (int i=1; i<=20; i++)
[self.modelArray addObject:[NSString stringWithFormat:#"Page: %d", i]];
self.pageViewController = [[UIPageViewController alloc]
initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
PageContentViewController *startupVC = [[PageContentViewController alloc] initWithNibName:#"PageContent" bundle:nil];
startupVC.pageLabel = [self.modelArray objectAtIndex:0];
[self.pageViewController setViewControllers:[NSArray arrayWithObject:startupVC]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
self.pageViewController.view.frame = self.view.bounds;
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController
{
// Relevant code to add another view...
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController
{
// Relevant code to add another view...
}
-(UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
// Setting a break point in here - never gets called
if (UIInterfaceOrientationIsPortrait(orientation))
{
// Relevant code to create view...
return UIPageViewControllerSpineLocationMin;
}
// Relevant code to create 2 views for side-by-side display and
// set those views using self.pageViewController setViewControllers:
return UIPageViewControllerSpineLocationMid
}
#end
This all works perfectly well as I mentioned earlier. The PageViewController's view gets shown. I can swipe pages left and right in both portrait and landscape and the respective page number appears. However, I don't ever see two pages side-by-side in landscape view. Setting a breakpoint in the spineLocationForInterfaceOrientation delegate method never gets called.
This is such a head-scratcher I have burned out of ideas on how to debug/solve the problem. It almost behaves like the UIPageViewController isn't responding to the orientation changes of the device and therefore isn't firing off the delegate method. However, the view gets resized correctly (but that could be just the UIView autoresizing masks handling that change).
If I create a brand new project with just this code (and appropriate XIb's etc) it works perfectly fine. So something somewhere in my actual project is causing this. I have no idea where to continue looking.
As usual, any and all help would be very much appreciated.
Side Note
I wanted to add the tag 'uipageviewcontrollerspinelocation' but couldn't because it was too long and I didn't have enough reputation (1500 required). I think this is a devious ploy on Apple's part to avoid certain tags in Stackoverflow... ;-)
Finally found the problem. It was something of a red herring in its symptoms, but related just the same.
Putting a break point in the shouldAutorotateToInterfaceOrientation: method was a natural test to see if the UIViewController was even getting a rotation notification. It wasn't which led me to Apple's technical Q&A on the issue: http://developer.apple.com/library/ios/#qa/qa1688/_index.html
The most relevant point in there was:
The view controller's UIView property is embedded inside UIWindow but alongside an additional view controller.
Unfortunately, Apple, in its traditional documentation style, doesn't provide an answer, merely confirmation of the problem. But an answer on Stack Overflow yielded the next clue:
Animate change of view controllers without using navigation controller stack, subviews or modal controllers?
Although my RootViewController was loading the PageViewController, I was doing it as a subview to the main view. This meant I had two UIViewController's in which only the parent would respond to changes.
The solution to get the PageViewController to listen to the orientation changes (thus triggering the associated spine delegate method) was to remove addSubview: and instead present the view controller from RootViewController:
[self presentViewController:pac animated:YES completion:NULL];
Once that was done, the orientation changes were being picked up and the PageViewController was firing the delegate method for spine position. Only one minor detail to consider. If the view was launched in landscape, the view was still displaying portrait until rotated to portrait and back to landscape.
That was easily tweaked by editing viewDidLoad as follows:
PageContentViewController *page1 = [[PageContentViewController alloc] initWithNibName:#"PageContent" bundle:nil];
NSDictionary *pageViewOptions = nil;
NSMutableArray *pagesArray = [NSMutableArray array];
if (IS_IPAD && UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
pageViewOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMid]
forKey:UIPageViewControllerOptionSpineLocationKey];
PageContentViewController *page2 = [[PageContentViewController alloc] initWithNibName:#"PageContent" bundle:nil];
[pagesArray addObject:page1];
[pagesArray addObject:page2];
}
else
{
[pagesArray addObject:page1];
}
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:pageViewOptions];
self.pageViewController.delegate = self;
[self.pageViewController setViewControllers:pagesArray
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];
Job done and problem solved.

Launch screen delay works in iOS Simulator but not Device

I've set my launch screen to only display for 1 second using the code below sleep(1); in the app delegate, but when testing directly on a device, I have to wait for the full 5 seconds.
Every time I run a test using an iPhone, or an iPad, I have to wait the full 5 second default before the app will load, however, it works great in the simulator.
If I unplug the iPhone cable, the sleep() function works on the devices. Is there a setting in xCode for this?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(1);
...
}
In order to delay the launch screen this is not the recommended method. This just delays the whole process.
I recommend you add a new viewController to the very beginning of your project which will show immediately after the launch screen with a UIImageView containing the exact same image as the launch Image.
Then add a delay from there before switching to the original first screen. This way you can even add a new different kind of transition to the launch image.
#import "OrginalController.h"
- (void) gotoOrginalFirstScreen
{
// This function will take you to your oringinal first screen
// from the temporary screen with the launch image
OrginalController *controller = [[OrginalController alloc] initWithNibName:#"OrginalController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// add this line to call the transition and the delay
[self performSelector:#"gotoOrginalFirstScreen" withObject:nil afterDelay:1.0];
}
you can display the launch screen without putting it to sleep itself. You don't need a separate view controller also. This method will work anywhere and wont trouble you also.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"splashscreen.png"]];
imageView.frame = CGRectMake(0, 0, 324, 480);
[window addSubview:imageView];
[self.window makeKeyAndVisible];
[self performSelector:#selector(firstscreen) withObject:nil afterDelay:1.0];
return YES;
}
-(void)firstscreen
{
// Load some View
}
this you can put it in you application delegate this will show your splash image for 1 sec after which the next screen will be loaded.

pushing nil rootViewController onto target <UINavigationController

I've been researching this all day but I haven't found anything about the rootViewController in relation to this error message. I know what the problem is but have no idea how to fix it. My problem is that my window.rootViewController is not connected or shows null and I can't figure out what to do. I've tried everything I could think of in code and in IB, but bad things happen whenever I change something. This is the message I get: "Application tried to push a nil view controller on target UINavigationController"
I can see the window.rootViewController from an NSLog statement:
"window.rootViewController : (null)"
of course, everything was working perfectly before upgrading my Xcode to 4.2 and ios5. :)
btw - the view loads but I cannot work any of the buttons, they do not light up at all. And my navigation works fine too.
here is my appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[DDLog addLogger:[DDTTYLogger sharedInstance]];
NSLog(#"Viewcontroller : %#", self.viewController);
// Set the view controller as the window's root view controller and display.
//self.window.rootViewController = self.viewController;
//do it this way, previous version not supported in ios5 - may need to check version for compatibility
[self.window addSubview:self.viewController.view];
//set up navigation controller
NSLog(#"window.rootViewController : %#", self.window.rootViewController);
navigationController = [[UINavigationController alloc]
initWithRootViewController:self.window.rootViewController];
navigationController.navigationBarHidden = YES;
NSLog(#"navigationController : %#", navigationController);
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
//force this view to be landscape
[application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
[self.navigationController.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)];
[self.navigationController.view setFrame:CGRectMake(0, 0, 748, 1024)];
[UIView commitAnimations];
return YES;
}
Thank you.
According to the docs:
Discussion
The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.
Double check your nib file to make sure it is connect.
Although the "Application tried to push a nil view controller on target UINavigationController" sounds like you maybe losing your VC reference. How is the property set for this? Is it retained?
Here are the docs.
http://developer.apple.com/library/ios/documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/doc/uid/TP40006817-CH3-SW33