Using multiple Storyboards in Xcode - ios

I have created 5 extra Storyboards and I have deleted the main storyboard. This is because I would like to have 5 separate storyboards for the iPhone and iPad to match 5 different screen sizes. So far I have one for the iPhone 3.5 inch, one for the iPhone 4 inch, one for the iPhone 4.7 inch, one for the iPhone 5.5 inch and one for the iPad. I have put the code bellow in to link it all up and make it work. However, it doesn't work when you try and build the project. There is no errors but lets say I go into the iPhone 3.5 inch storyboard and I add a UIViewController and a Button or label, then when you build the project it goes to your launch screen and then it doesn't do anything from there. I have put the starting arrow in at the UIViewController but I cannot get anything to come up apart from the launch screen. I have tried this on all the storyboards and their simulators. I am wondering if I have missed something out in the code but I am not sure.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *storyboard = nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPad) {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPad" bundle:nil];//iPad
} else {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height == 480){
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone4S" bundle:nil];//iPhone 3.5inch
} else
if (screenSize.height == 568){
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone5/5C/5S" bundle:nil];//iPhone 4inch
}
else
{ if (screenSize.height == 667){
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone6" bundle:nil];//iPhone 4.7inch
} else
if (screenSize.height == 736){
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_iPhone6Plus" bundle:nil];//iPhone 5.5inch
} else
//default storyboard
storyboard = [UIStoryboard storyboardWithName:#"Main.Storyboard" bundle:nil];
}
}
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}

Find your info.plist file in your project and remove the line called "Main storyboard file base name".
Also, you forgot to create the window (credits to rdelmar).
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
I strongly advise you to look into Auto Layout and Size Classes. Using these two, you can support all screen sizes within one single Storyboard. It's a bit hard to get in the beginning, but it'll definitely be worth it in the long run.
There's a video about this from the most recent WWDC called Building Adaptive Apps with UIKit.

Related

Apple approve multiple storyboard instead of Size class?

I have a project with complex design, so i prefer to use different storyboard according to size of the iPhone device, will apple approve it ?
Is any restriction to force to use auto layout?
How can i use minimum storyboard to achieve this task without using auto layout and size class?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// int screenHeight = [UIScreen mainScreen].bounds.size.height;
// NSLog(#"Screen Height is %i", screenHeight);
// grab correct storyboard depending on screen height
UIStoryboard *storyboard = [self grabStoryboard];
// display storyboard
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
- (UIStoryboard *)grabStoryboard {
// determine screen size
int screenHeight = [UIScreen mainScreen].bounds.size.height;
UIStoryboard *storyboard;
switch (screenHeight) {
// iPhone 4s
case 480:
storyboard = [UIStoryboard storyboardWithName:#"Main-4s" bundle:nil];
break;
// iPhone 5s
case 568:
storyboard = [UIStoryboard storyboardWithName:#"Main-5s" bundle:nil];
break;
// iPhone 6
case 667:
storyboard = [UIStoryboard storyboardWithName:#"Main-6" bundle:nil];
break;
// iPhone 6 Plus
case 736:
storyboard = [UIStoryboard storyboardWithName:#"Main-6-Plus" bundle:nil];
break;
default:
// it's an iPad
storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
break;
}
return storyboard;
}
Short answer
Yes
Yes, Apple will approve the app.
However I would suggest using 1 storyboard instead of multiple storyboards.
Cons of using multiple storyboards are
It take 200% more time than actual time. Think tomorrow when you will have more iPhones.
It makes app size more bigger
Worst part is it take more time for compile while working on the project & take more time when switching storyboards.
The most bad thing is it have more chances of missing some changes in some storyboard.
YES! Apple will accept your app.
To help you factorize your design, you can use storyboard references to reuse storyboard parts common to every device.
http://code.tutsplus.com/tutorials/ios-9-staying-organized-with-storyboard-references--cms-24226
(Only available from iOS 9)
yes definitely apple will approve it, I have used it in one of my apps. But it may increase the memory consumption.

I need to set a separate Main Interface for iPhone and iPad, but Xcode only lets me set one interface for the project when it is universal

I am unable to set a specific Main interface for iPad, even though my app is universal. Xcode used to have separate tabs for the Deployment Info on iPhone and iPad but now they are they same. However my game is radically different on iPhone and iPad and I need to set separate storyboards for each interface.
Someone please help if you know the solution.
you could set the value in the Deployment Info -> Main Interface to an empty string and implement a custom logic in your AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard *initialStoryboard;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
initialStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
} else {
initialStoryboard = [UIStoryboard storyboardWithName:#"Main_iPad" bundle:nil];
}
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [initialStoryboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
You need to use size classes for this. To select the correct size class, open your storyboard file, then click on this icon towards the bottom of the screen.
Clicking wAny hAny will allow you to change the size class type and set specific constraints for the different device types. You can read more information about size classes here

Why won't my storyboard work on the iphone?

In the app delegate I have this written to determine which storyboard will be opened depending on the screen size,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [self grabStoryboard];
// show the storyboard
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
- (UIStoryboard *)grabStoryboard {
UIStoryboard *storyboard;
// detect the height of our screen
int width = [UIScreen mainScreen].bounds.size.width;
if (width == 480) {
storyboard = [UIStoryboard storyboardWithName:#"iphone4" bundle:nil];
// NSLog(#"Device has a 3.5inch Display.");
} else {
storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
return storyboard;
}
The app opens landscape on the device.
This will properly choose between a 3.5 inch device and a 4 inch device in the simulators, but won't properly display in an actual 3.5 inch device. In Xcode 6 it opens the correct storyboard for the 4s or the 5, 6 ,6+ depending on the width of the screen but when tested on an actual Iphone 4s (with ios 7.1.1) the storyboard configured for 4 inch devices is displayed.

iPhone 6 / iPhone 6+ launch screen size issue

I want to optimize my app for iPhone 6 and iPhone 6+
But I have a launch screen size problem.
I created a simple project, and AppDelegate's code is:
#import "AppDelegate.h"
#import "ViewControllerOne.h" //xib screen size is 4 inch.
#import "ViewControllerTwo.h" // xib screen size is 4.7 inch.
#import "ViewControllerThree.h" // xib screen size is 5.5 inch.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
if ([UIScreen mainScreen].bounds.size.height == 568.0) {//4 inch
ViewControllerOne *first = [[ViewControllerOne alloc]init];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:first];
self.window.rootViewController = navigation;
}
else if ([UIScreen mainScreen].bounds.size.height == 667.0){//4.7inch
ViewControllerTwo * second = [[ViewControllerTwo alloc]init];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:second];
self.window.rootViewController = navigation;
}
else if ([UIScreen mainScreen].bounds.size.height == 736.0){//5.5inch
ViewControllerThree *third = [[ViewControllerThree alloc]init];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:third];
self.window.rootViewController = navigation;
}
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#end
When I run iPhone 6 or iPhone 6+ simulator, it always appears 4inch screen when I run this project.(I included three launch image(Default-667h#2x.png, Default-736#3x.png, Default-568#2x.png) also.
What I have to do to resolve this problem.
I checked others question that similar to mine, and used their code, but all of those not works. That is maybe my technical skills relatively row.
Once you specify the images in Assets Catalog you will see the right images
Go to YourAppTarget> General> App Icons and Launch Images> Launch Image Source > Use asset Catalog
and then drag the appropriate size for each device.
In addition to adding images you can also add a Launch Screen File. This is a .XIB file that allows you to specify constraints in addition of launch images. It only works on iOS8. To support iPhone 6 and 6+, we only add this file and specify constraints:
To set the Launch Screen File add it to your project:
File > New > User Interface > Launch Screen
Then go to: YourAppTarget > General > App Icons and Launch Image s> Launch Screen File > Set your newly created image
You will still need launch images for Retina 4" and 3.5", if you are supporting iOS 7.

"Application windows are expected..." error when loading storyboard for Ipad

I'm loading different storyboards for Iphone and Ipad and the Iphone's storyboards load good but when I start the Ipad Simulator I got the "Application windows are expected..." error.
this is the code in AppDelegate:
#implementation AppDelegate
- (UIStoryboard *)grabStoryboard {
UIStoryboard *storyboard;
// detect the height of our screen
int height = [UIScreen mainScreen].bounds.size.height;
if (height == 480) {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_Iphone3" bundle:nil];
// NSLog(#"Device has a 3.5inch Display.");
}
if (height >= 1024 && height <= 2048 ) {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_Ipad" bundle:nil];
NSLog(#"Device has a 4inch Display.");
}
else {
storyboard = [UIStoryboard storyboardWithName:#"Storyboard_Iphone4Up" bundle:nil];
// NSLog(#"Device has a 4inch Display.");
}
return storyboard;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *storyboard = [self grabStoryboard];
// show the storyboard
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
In the console I see the NSLog message but the storyboard doesn't load and the "Application windows..." error appears.
I'm using some Scroll Views and some elements but i thinkg those are not the problem because there is an option for them "Adjust Scroll View Insets".
I uploaded some screen shots to show more about my project. Thanks for your time.
I figured it out. When you copy all stuff from the Iphone storyboard to the Ipad one the Initial View Controller checkbox is unchecked. Just check it and all will be good (:

Resources