xcode6 single storyboard but different images required - iphone

I making my first universal app using xcode6 with the single storyBoard. Usually I have two storyBoards - one for iPhone and one for iPad. Each storyBoard would have its relevent images.
But now, if I have UIImage as a background on the iPad, how can I have have a differnt uiImage on the iPhone ?

Solution # 1)
You can use the "UI_USER_INTERFACE_IDIOM" conditional to help you out.
Based on the code in this answer, all you'd need to do is set your UIImage to an outlet and then do something like:
- (BOOL) isPad{
#ifdef UI_USER_INTERFACE_IDIOM
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#else
return NO;
#endif
}
if([self isPad])
{
//do code for iPad
[self.backgroundImageView.image = [UIImage imageNamed:#"bigBackgroundImage"];
}
else
{
//do code for iphone
[self.backgroundImageView.image = [UIImage imageNamed:#"smallerBackgroundImage"];
}
Solution # 2)
You can indeed still use separate storyboards for iPad and iPhone.
To do this, you need to edit your app's info.plist file to show two different storyboard files for iPad and iPhone. In Xcode 6.1, it looks like this:
or, if you were editing the Info.plist file directly, you'd take out the original UIMainStoryboardFile` key and value and insert these two instead (rename the files to whatever you want them to be...)
<key>UIMainStoryboardFile~ipad</key>
<string>nameOfiPadStoryboard</string>
<key>UIMainStoryboardFile~iphone</key>
<string>nameOfiPhoneStoryboard</string>

Related

Converting an iphone application into an iPad , iOS?

I currently have an iOS iPhone application.
What i want to do , is make it universal so that i can target the iPad too.
What i did , was go to the target and change the iOS application target , from iPhone to universal.
Now when i run the application on my iPad , it automatically resizes all the views for the iPad.
However there are some views , with background pictures that dont look so good , cause i need to use higher resolution pictures or in general i should change some things in the iPad version.
Where are the iPad .nib files??? I mean , i only see the iPhone ones. When i run it on my iphone , these files are used. When i run it on my iPad everything is resized correctly , but where the hell are these .nib files?
The tutorials (pretty old) that i read , suggested that when you target the iPad too , new .nib files should be created exact copies for the ipad. Why i dont see these files?
You can have iOS automatically load the right xib based on the extension, akin to how Retina graphics work. If your xib is named Awesome, and you want to convert it into having an iPhone and an iPad version (instead of being shared, rename it such that:
iPhone version:
Awesome~iphone
iPad version:
Awesome~ipad
Then, when you tell iOS to load Awesome, it'll pick which one to load based on the current platform automagically. No need for if statements in your code! You can still if you want, but it's not required.
Note: You might need to perform a clean after the rename! Sometimes some files stick around in the build when renamed.
You will just need to make new .xib files and set them to the same class and you can init that viewController with a condition:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
yourVC = [[YourViewController alloc] initWithNibName:"YourViewController_ipad" andBundle:nil];
}
else
{
yourVC = [[YourViewController alloc] initWithNibName:"YourViewController" andBundle:nil];
}
Whenever possible you should try to use the same .xib but in a lot of cases it isn't possible to do that and look good so you just make a second. Xcode won't do it automatically for you.
Let's say that you have a class. We'll call it Two.
Here are the current files that make up the Two class.
Two.h
Two.m
Two.xib
Two.xib contains a UIView sized for the iPhone. In order to make a view sized for the iPad, you should create a new XIB file (name it Two_iPad.xib), connect the XIB to Two, resize the UIView in Two_iPad.xib for the iPad, and design accordingly.
When you are creating a new instance of Two, do the following.
Two *two;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//they are using an iPad
two = [[Two alloc] initWithNibName:#"Two_iPad" bundle:nil];
} else {
//they are using an iPhone/iPod Touch
two = [[Two alloc] initWithNibName:#"Two" bundle:nil];
}
You are creating a new instance of Two; however, you are checking which device the user has, and using the corresponding XIB file.

Xcode Image Background Swap Device Dependent

Currently I'm defining my app's background image in XCode's Attribute Inspector under Image View.
I'd like to, however, use a different background when an iPhone 5 views the app -- this is for a gaming app so certain screens have tables that I want to make longer. I know this has been written about a lot and some if/then statements are required. My question is: If I've already defined the background image using the Attribute Inspector, is there code to overwrite that which I can add to my .h and .m files?
Here's what I'm thinking: This is the code I'd add to my AppDelegate file:
#define IS_WIDESCREEN (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)
#define IS_IPHONE ([[[UIDevice currentDevice]model] isEqualToString:#"iPhone"])
#define IS_IPOD   ([[[UIDevice currentDevice]model] isEqualToString:#"iPod touch"])
#define IS_IPHONE_5 (IS_IPHONE && IS_WIDESCREEN)
This is the code I'd use in my .m file:
if(IS_IPHONE_5) {
} else {
}
So here really is what I'm looking for:
Where do I put this code -- do I have to define a new (void) statement?
What code do I use between the { } to call that new image and tell it to display as the background?
simply take the image you want to display instead of your already existant a "HomeBG-568h#2x.png" with size 640 x 1136 so xcode will automatically use that image when the device is an iphone 5 anywhere your app calls for the "HomeBG.png" image. Hope that helps!

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.

iphone to ipad xib file change

I have the two xib files 1.main 2.language translator this is a iphone app.
I want to change UNIVERSAL APP. i upgrade the universal application. now automatically created mainwindow-ipad.xib.
i run this app in xcode 3.2 version but it display left corner only .
how can i change this app to universal ... its big headache to me.. anyone help me
Thanks in advance,
Suresh.m
Check whether app is on iPhone or on iPad and accordingly load the nibs, I mean u have to keep the Ipad versions of ur interface builder files and load them according to the target device
if([[UIDevice currentDevice]interfaceIdiom]== UIUserInterfaceIdiomPad)
{
//Load ur ipad nib file
}
else
{
//Load ur iphone nib file
}
I usually create a new universal project, and then "move" my code into the template provided. I find that much easier then trying to flip all the other knobs.
I found iphone_bharat's example interesting, but it has a small type so I had to fix it and clean it up a little bit.
+ (bool)iPad
{
// Can use UI_USER_INTERFACE_IDIOM() for iOS less than 3.2
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad )
return YES;
else
return NO;
}