In the old XIB world of iOS development, I was able to create one View Controller and one XIB, and use them for both the iPhone and iPad environments. With just a few code tweaks, the XIB would resize and adjust to fit the different dimensions and aspect ratios. This allowed me to reduce the risk of the two layouts getting out of sync over time.
Is the same thing possible in the world of Storyboards? By default Xcode creates two distinct storyboards for my Universal app -- one for the iPhone and one for the iPad.
How can I use one storyboard for both?
Thanks!
Updated:
Size Classes in Xcode 6 will essentially achieve the goal of Universal Storyboards. More info here:
https://developer.apple.com/library/prerelease/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_6_0.html
In short...you can't.
There's a workaroud I use to create the iPad storyboard at the end of the iPhone development.
You have to locate the iPhone story board and using the Terminal you can duplicate it giving it the name of the iPad storyboard that Xcode has created for you. Delete the empty old storyboard and open the newly copied one changinge the targetRuntime attribute to "iOS.CocoaTouch.iPad" instead of the old value "iOS.CocoaTouch".
Open the iPad storyboard with Xcode, everything is there but it still needs to be arranged and connected to the ViewController (wiring stuff included..). It's not perfect but I usually do it to save some error prone copy and paste (and several hours as well).
If you just want to target your app for iPhone and deliver it to iPad you can just use the iPhone Xib and you'll get the 2x little botton in your right corner.
Update late 2014 (after Xcode 6 beta release)
Xcode 6 now allows you to define a single storyboard and through the size classes concept, to make it adapt to all their devices. This solution is a bit confusing (especially if you're familiar with how this problem is solved in the Android environment) but it's something in the right direction IMHO.
Yes you can.
You can simply select the very same storyboard in Xcode under General/Deployment info
I have a few app which have that.
They share a lot of view controllers.
If you use freeform size than you can create separate view controller in the story board and two separate segues.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self performSegueWithIdentifier:#"Open VC" sender:sender];
} else {
[self performSegueWithIdentifier:#"Open VC iPad" sender:sender];
}
Only disadvantage is that you cannot use iPad specific controls directly from the storyboard (popover etc.) unless you switch to an iPad storyboard
Related
I have a project for iPhone and iPad. The iPad needs a split view controller. Do we create two separate storyboards? One for iPhone (using autolayout we can support all devices) and two for iPad. My doubt is the difference it only in initial view. The inside views repeats. How will be your approach with storyboard ?
No need for two storyboards any more in iOS 8! We can use Adaptive UI in order to tailor the same storyboard all different sizes of iPad and iPhone.
You can find a nice tutorial in here http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial
And also you can find a nice video by apple in WWDC conference here and search for "Building Adaptive Apps with UIKit"
Basically we can handle different screen sizes by defining elements of our UI in storyboard for different size classes. We can also define different Auto layout constraints for different size classes.
And these are all possible size classes in iOS 8 ( I took the image from https://medium.com/#getaaron/ios-8-development-tips-for-iphone-6-and-iwatch-1c772554ffe0)
For iOS 7 and before, yes, use two storyboards and two completely different interfaces, since there is no split view controller on the iPhone.
For iOS 8, use one storyboard, and use the UISplitViewController on both the iPad and the iPhone. Make a new project from the Universal version of the Xcode 6 Master-Detail app template to see all about how it works! It is automatically a split view controller on the iPad and a navigation interface on the iPhone.
Even with adaptive UI, personally I prefer to work on different storyboard. For complex layout it is easier this way and it will not easily break the layout for other screen size. Having too many constrain can make harder to maintain. Also moving the object hierarchy will break the layout for other screen.
As bonus, if you work with low level mac like mackbook air or old mac, it also reduce the load time and get better responsiveness when you work with storyboard.
On iOS 8, you do not need two storyboards since a single storyboard can handle both iPhone and iPad; also, UISplitViewController is supported on the phone on that OS.
For earlier releases of iOS, you'll need two storyboards.
You can have many more than just two storyboards. If you are unable to us size classes / auto layout to accomplish the job of a view controller shared between iPhone and iPad, you can split that part into separate storyboards. You can then create a third storyboard which holds the view controllers that are shared. You can then instantiate that storyboard in code and use it to instantiate its view controllers.
I managed to have that scenario (split controller in an universal storyboard that works on both iOS8 and iOS7) by calling a different segue while on iOS7 and iPhone.
My scenario is like this:
I have a login controller that is supposed to segue to a main controller (modally).
When on iOS8 or iPad I use a segue points to the split view controller, while when on iOS7-iPhone I use another segue that points directly to the left nav controller of the split controller (bypassing it entirely).
If from the left controller you have a detail segue that points to the right nav controller(and you should) and if the segue type is "Show Detail" (and it should) then it will perform like a regular push in the iOS7-iPhone environment (which is exactly what we want).
On thing I noticed though, the detail segue's destination controller in the IOS7-iPhone scenario becomes the root view controller of the right nav (even though the segue is pointing to the nav), therefore, if you have some code in the prepareForSegue you might need to adapt it to handle them differently)
(For determining if the platform is IOS8 and if I'm on iPhone/iPad idiom I use the standard
[[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 and UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
I have Xcode 6 beta installed and I'm trying to develop an universal app. Before Xcode 6, you had to create 2 separate Storyboards for iPad and iPhone and you could set it in the Deployment Info.
In Xcode 6, it seems that separation is gone. There aren't 2 tabs to set the storyboards individually.
But when you go to create a Storyboard, you are asked to choose a device family.
Can someone explain what's going on in Xcode 6 please?
Thank you.
To support the new Size Classes, you'll need to enable "Use Size Classes" in the File Inspector of your storyboard. This will allow you to configure your storyboard for multiple device sizes.
Note that this will make your storyboard incompatible with Xcode 5.
When you've enabled this, you'll see the size selector appear at the bottom of the screen. Use this to select your device size:
In your project preferences, you can still select a different storyboard for iPhone or iPad using the dropbown box. Notice that the storyboard name will persist if you select a different one for each device.
Even though this is still an option, Apple is moving developers towards a single, unified storyboard.
It seems Auto layout is used to support all devices now, yet you can create separate storyboards? Have you tried calling the separate storyboards in code in the app delegate
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//Use iPhone Storyboard
} else {
//Use iPad Storyboard
}
After Xcode 6 > version for using different storyboard for iPhone and iPad need to follow xcode-6-separate-storyboard-for-ipad-and-iphone
Note : For Application which does not use Auto Layout, so while adding new storyboard for iPad and disabling auto layout, a pop will appear that's where you need keep size class data to iPad as shown in below screen.
I'm starting a project that must be for iPhone and iPad...you can tell me that it's easy and that I can create an "Universal" project..it's right.
But I have a "stupid" question about a "stupid" problem.
In an universal project I have two .xib (iPhone and iPad) for a class and inside code I verify if I'm using the one or the other
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
}
else
{
// The device is an iPhone or iPod touch.
}
ok?
But you know that in a single view of iPad I can show many views that an iPhone can show one at time.
At example I have two view with their classes:
FirstView.h-FirstView.m-FirstView_iPad.xib-FirstView_iPhone.xib
and
SecondView.h-SecondView.m-SecondView_iPad.xib-SecondView_iPhone.xib
For iPhone it's not a problem but for iPad? In a view in iPad I can put first and second viewcontroller, it's not easy to organize if I have two different classes for two viewcontrollers... Do you have a solution for this? Or the best solution is crate two separate project iPhone and iPad?
thanks
So yeah I always use one theViewController.h and one theViewController.m and I have a theViewController.xib for iPhone and a separate xib for iPad only. if you name the iPad xib ending with theViewController~ipad.xib the OS will automatically recognise the XIB. You will have to set the class of the theViewController~ipad.xib in Interface builder in the inspector area to the same class as your iphone xib i.e the class would be called theViewController then you can connect up what ever outlets you have defined in your .h and .m also the great thing about just naming the iPad xib to ending with ~ipad.xib when you push views you dont have to check the idom before pushing as the OS recognises the ~ipad.xib extension and pushes the correct view depending on device..hopefully this is helpful for you!
I am making a new Objective C class and it gave two options as check boxes:
1) Targeted for iPad
2) Make an XIB interface
What if I want to make an app that would work on both iphones and ipads? Can I do that or will I essentially need to re-do the app, so if I am testing it out on the phone first, I should not check that box? Or will that box just make things compatible with the iPad?
Also, for the XIB interface - its where I can place the ui elements, right? But can I place the UI elements on the button right from the storyboard?
Thanks!
As far as I know it will create XIB files for iPhone and for iPad screen sizes.
You don't need to make 2 apps, you can use 1 App logic for both and just change the views.
Few notes
This is one places were the MVC is important.
In the project summery on xcode you should mark the app as universal.
an other few lines that can help you are:
+ (BOOL)isDeviceAniPad {
#ifdef UI_USER_INTERFACE_IDIOM
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#else
return NO;
#endif
}
You can use this code to check the user device any time your code needs to perform different operations for iPhone and iPad.
An other option will be to create 2 Targets - one foe iPad and one for iPhone and let the targets share the same code.
Yes, XIB interfaces, like storyboards, are where you can interactively drop the UI elements. XIBs are a more primitive version of storyboards (which came in with iOS 5), but at the same time if you're working on a project with many hands and developers working on a single project, or lots of screens and view controllers (which would make for a very hefty & unwieldly storyboard), it may be worthwhile to stick with xib files.
If you select "Targeted for iPad", you may only get a storyboard sized for iPad-sized screens.
If you select "Make a XIB interface", your new class will be using xib files instead of storyboards.
I have a storyboard for my iphone version of my app and I want to have the same storyboard for my ipad version (only bigger). My story board only has text and buttons is there anyway I can just convert the whole thing and increase the size by 2, or do have to resize everything manually?
You really need to do more than just resize you know. iPad users don't generally enjoy running iPhone apps at double size on the iPad and it sounds like that's what you're proposing to do.
In your storyboard you can drag and drop from your iPhone screens to iPad to get all of the elements into the iPad storyboard. Then maybe you will tweak a bit to make it more iPad friendly.
As far as I know, you will need to create a new storyboard and set that as the storyboard for iPad (app settings). You will then need to create the content manually (you can copy and paste the objects and use the existing controller classes though (will need to wire up the objects).