问题
I'm trying to write an app without using nib, everything I'll do it programmatically.
Now the problem is, how am I going to support both iPad and iPhone? Obviously, I can't do it with
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
// load iPad nib
} else {
// load iPhone nib
}
If I create 2 ViewControllers, then the IBAction
will be redundant.
Any suggestion?
回答1:
You should probably just figure out the device type in applicationDidFinishLaunching
and then load separate controllers for each device. But if you want to just have a single implementation for all devices, do checks like this:
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
//do some iPad stuff
}
else
{
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
if([UIScreen mainScreen].scale == 2.f && screenH == 568.0f)
{
//do iPhone 5 stuff
}
else
{
//do iPhone 4S and iPhone 4 stuff
//the dimensions are the same however, if you want to do iPhone 4S specific stuff
// you'll need to do additional checks against video resolution or other differences etc
}
}
回答2:
CGFloat height = [UIscreen mainScreen].bounds.size.height;
if(height==568.00 || height == 480.0)
{
//For iphone 5 and iphone 4
}
else
{
//For ipad
}
回答3:
You can use this code in your AppDelegate
- (BOOL) isPad()
{
if(UI_USER_INTERFACE_IDIOM)
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
}
else
{
return NO;
}
}
This link gives some info about the idiom http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/macro/UI_USER_INTERFACE_IDIOM
OR
You can check the height and width of the screen to know whether its an iPhone or iPad
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
回答4:
If you are not using any nib, everything doen programmatically you dont want to create two view controllers for the iphone and ipad. Remember do not depends on any static values. ie your calculations should be made according to self.view.bounds
some thing like that (I mean to create views/subviews). Then if some specific functionality that supports only in iPad do checks
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
One view controller done all the job for you.
来源:https://stackoverflow.com/questions/15401659/ios-support-ipad-iphone-without-using-nib