Xcode Image Background Swap Device Dependent - iphone

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!

Related

xcode6 single storyboard but different images required

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>

Is it possible to create application with half part compatible for iphone only and other part is for universal?

I am developing an application, in it as the client requirements, few views are compatible for only iPhone 4 devices and other views are compatible for both like iPhone 4,5 /iPad. So is it possible in single build?
I know it is not possible but I want your suggestion for the same.
If you have any link related to that then please share with me.
Yes, it is possible to have different views for iPhone 3.5", iPhone 4" and iPad. Also, you can implement different features in every different device. However, iOS User Interface Guidelines recommends that you provide same functionality across different devices.
Preserve the primary functionality of your app, regardless of the device it runs on. Even though one version might offer a more in-depth or interactive presentation of the task than the other, it’s important to avoid making users feel that they’re choosing between two entirely different apps.
In practice, you can programmatically write a bifurcation to build (programmatically) a view using this macros:
#define isIpad() \
([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)] && \
[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad)
#define isWidescreen ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 )<DBL_EPSILON )
#define isIphone ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: #"iPhone" ] )
#define isIpod ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: #"iPod touch" ] )
#define isIphone5 ( isIphone && isWidescreen )
Also, you can create different xib files for every different devices, using the bifurcation and UIViewController message: - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil, to load the correct file.
Finally, you can use modifiers in the xib filenames to load different resources between iphone and ipad. For example: You have FirstViewController.xib for your iPad UI; you can create a FirstViewController~iphone.xib that will be loaded automatically by iOS if the device is an iPhone (instead of FirstViewController.xib). You can check this answer for more information about this subject.
EDIT
In the case that you have already "iPhone only xib files" and you want to run those on iPad, you should consider to copy each of those files, and convert the copies to iPad target as this or this answers explains . Then, you can re-name this copies ending with "~ipad.xib", that should solve your problem, however, this views will need a layout revision.
just decide this by code. if a view shall be available for iPhone, call it, and if not just let it be. to distinguish between iPhone and iPad just use
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// iPad
else if([[UIScreen mainScreen] bounds].size.height == 568)
{
// iPhone retina 4 (iPhone 5)
}else
// Older iPhones

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

precompiler for ipad

HI all,
In xcode I am trying to load different nib file in case I am using iphone/ipod or ipad.
I have a Define.h file with all enum and constants in it, so I would like to put in there a kind of (obviously wrong because I don't know how to do it):
#if IPHONE // <-- can I intercept the Active Executable here ?
#define MY_VIEW #"TableViewIphone.xib"
#else
#define MY_VIEW #"TableViewIpod.xib"
#endif
Then in my view controllers which include Define.h
[[NSBundle mainBundle] loadNibNamed:MY_VIEW owner:self options:nil];
I am in need of doing this with device simulator, is there a way to intercept what's in "Active Executable" in xcode and have a specialized set of define ? Are there any other and easier alternatives ?
thanks
What you want to do is select a xib at run-time, appropriate for the device.
What you are doing is a compile-time selection, which will set the xib to be used for all devices, which is not what you want.
I'm assuming you're building a Universal app, yes?
I prefer to have the device load the appropriate xib when my app is launched by using the proper key in my app's Info.plist file, which is the key NSMainNibFile~ipad. (e.g. I set NSMainNibFile~ipad to MainWindow-iPad... note: no ".xib" bit)
Another alternative is to use the UI_USER_INTERFACE_IDIOM macro:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Load iPad xib
}
else
{
// Load iPhone xib
}