How to make iPhone and iPad version of an app? - iphone

I am trying to make an app which works on both iPhone and iPad. I am looking how to make an interface compatible on both. When the app loads I am displaying a table view. How can I load different nibs based on device? I am using this to switch between nibs.
if ([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)])
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
device = #"iPad";
}
else
{
device = #"iPhone";
}
}
But in MainWindow.xib it says view is loaded from the view controller for iPhone. Can I make this dynamic based on device? ie I want to show from the start of app different nibs based on device.
Thanks.

Actually, Apple does all this automatically, just name your NIB files:
MyViewController~iphone.xib // iPhone
MyViewController~ipad.xib // iPad
and load your view controller with the smallest amount of code:
[[MyViewController alloc] initWithNibName:nil bundle:nil]; // Apple will take care of everything

You can do that in a similar way:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
mainMenu = [[MainMenu alloc] initWithNibName:#"MainMenuiPad" bundle:nil];
}else{
mainMenu = [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
}

For making universal app,
1-set target family in info build tab for app as iPhone/iPad.
2- Delete window from main window.
3- Add two xib one for iPhone and one for iPad(by selecting iPad xib).
4- make appDelegate class as controller file for these xib's.
5- Add window on these xibs and view controller or navigation controller and by IB Inspector set load nib name and controller file here which one is your first view.
6- And then make differnet xib for iPad and iPhone which having tableview or other controls.
7-Make single contoller file or different controler file for different device for same you need to check the device by this if else condition
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
8-Now you need to load xib in appDelegate class in method didFinishL--
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
// load your nib for iPad here which having view controler or navigation controller as well window.
}else{
//load nib for iPhone.
}

Try this in your Info.plist:
Main nib file base name [NSMainNibFile]: MainWindow_iPhone
Main nib file base name (iPad) [NSMainNibFile~ipad]: MainWindow_iPad
I hope this helps you.

Related

How to add xib for iPad

I have created my app for iPhone and I have one xib. If I run my app on iPad simulator, it appears in left top corner.I want to add one more xib for iPad so that when I run iPhone simulator it will run iPhone xib and when i run iPad simulator it will run iPad xib. How can I do that?
To create only a Xib file just follow this.
FIle->New ->File...
In the window that appears click the User Interface under IOS u can see a Empty UIBuilder file.
click it then click next and give it the appropriate classfile name for ipad or iphone
First create a new xib for IPAD and while giving the name of the nib file provide the XIB file name created for IPAD.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
HomeScreenController *homeScreenController = [[HomeScreenController alloc] initWithNibName:#"HomeScreenController" bundle:nil];
[self.navigationController pushViewController:homeScreenController animated:YES];
}else {
enter code here
}

Auto-resize iPhone xib app to ipad

I built an iPhone app "using xib" with 5-6 screens. Now I want to auto-resize the display for iPad. I am using xcode 4.6.
Do I have to rebuild the entire code using storyboards? It will be a very tedious work. Is there any solution for this?
You'll need to create only new xib files for iPad and name them as ViewController_iPhone.xib and ViewController_iPad.xib and when switching your views, just put a simple condition
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
ViewController *viewController = [[ViewController alloc] initWithNibName:
#"ViewController_iPad" bundle:nil];
} else {
ViewController *viewController = [[ViewController alloc] initWithNibName:
#"ViewController_iPhone" bundle:nil];
}
use autolayout and everything will be done automatically
if not autolayout, then making 2 xib will be a better option. Make ipad size xib with the same name and put ~ipad after classname.
Like if you xib name is myClass.xib and create other one like myClass~ipad.xib.

Making universal build for iphone application with xcode 4.51

I had an old project which uses XIB for each UIViewControler.That was intended for I Phone but now I want to make that project with universal build that is for Iphone as wall as IPad also.The problem is that when I changed the build type to Universal and used coding like
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
// ipad coordinates
}
else
{
//iphone coordinates
}
this the display is correct on iphone and ipad but on ipad the buttons are not taking click event on entire button but only on a small portion of button.Though I am not loading Nib but it is taking crated iphone xib file internally and I think thats why it's button behavior is not in accordance to ipad view. I am confused that what I should do?How to make copy of XIB for Ipad and load them depending upon device.It is to mention here that I am not loading any nib from my code but It is being called internally. Please help
For universal app you can make 2 xib
1 - > yourView_iPhone
2 - > yourView_iPad
And load each xib according yo your idiom
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
yourView *obj = [[yourView alloc]initWithNibName:#"yourView_iPad " bundle:nil];
// ipad coordinates
}
else
{
yourView *obj = [[yourView alloc]initWithNibName:#"yourView_iPhone " bundle:nil];
//iphone coordinates
}

Can i connect 2 nib files to the same .h & .m?

I'm trying to make a universal app, so i was wondering if i can connect two nib files ( 1 for iPad and the other for iphone ) to the same .h and .m files ?
i have 3 files TestView.h TestView.m and TestView.xib.... how can i connect a TestView_iPad.xib to the same TestView.h and TestView.m ?
i'm new to Xcode and i'm using Xcode 4 right now
thanx in advance :)
Short answer: Yes.
Provided you follow the model/view/controller style, you can re-use the same View and Viewcontroller (.h and .m) files in both an iPad nib and an iPhone nib (or storyboard). There will be occasions when you need to use the following type of code, though:
BOOL iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
if (iPad) {
.... iPad specific code like SplitViewController
} else {
.... iPhone / iPod Touch specific code
}
You may also need to check if the view controller you are in is on-screen (as on the iPad more than one ViewController can be onscreen), in which case use:
if (self.view.window) {
.... ViewController onscreen so do something otherwise do nothing
}
Also don't hardcode the bounds of the device's screen. Use the following to find your screen size (in points):
CGRect screenBounds = [[UIScreen mainScreen] bounds];
Hope this helps.

Loading xib dynamically but wrong one displaying

I have an universal app that I am trying to share a viewController code with. I have this:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
AboutController *screen = [[AboutController alloc] initWithNibName:#"iPhoneAboutController" bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
}
else
{
AboutController *screen = [[AboutController alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
}
Although this is loading, and when I step through the code, it does hit the xib for the iPhone but it seems to always be loading the iPad version. I know this because in the xib file for the iPhone, I have manually added different background images and it never shows. In the iPhone simulator it shows the iPad version where it is off screen.
Also, if I step through the code in the controller, it does show that the load is the iPhone yet display is all iPad objects. In the iPhone xib, I do have the Files Owner set to the AboutController.
This is the first time I am attempting to "share code". I know I can just create separate class files with the same code but this seems senseless. Any help is greatly appreciated.
Geo...
For starters: make sure you don't override nib initialization in your AboutController.
If not, try cleaning your project (also delete your app's folders in ~/Library/Developer/Xcode/DerivedData). Also uninstall the app from device and then rebuild.