有没有办法检测用户使用的设备,然后使用该信息,让应用程序使用特定的nib
文件?
现在,我的代码是
- (BOOL)application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions{
if(isiPhone5){
self.RootViewController = @"RootViewController_iPhone5";
}
else{
self.RootViewController = @"RootViewController_iPhone4";
}
}
我查找设备的其他代码可以运行并告诉我用户使用的设备,但实际上并未将.xib文件从RootViewController_iPhone4.xib
更改为RootViewController_iPhone5.xib
。有没有办法做到这一点?
我不想使用自动布局,因为我希望根据设备发生其他自定义事件,例如视图控制器中的不同按钮名称,如果使用的设备是iPhone 4和iPhone 5
答案 0 :(得分:6)
试试这种方式..
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height ==568)
{
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone5" bundle:nil];
}
else
{
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone4" bundle:nil];
}
更新了答案
检查一下...... Black Screen for screen detection
答案 1 :(得分:1)
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (IS_IPHONE_5)
{
self = [super initWithNibName:@"YourViewController~iphone5" bundle:nil];
}
else
{
self = [super initWithNibName:@"YourViewController~iphone" bundle:nil];
}
return self;
}
尝试这样的事情
答案 2 :(得分:1)
您可以通过以下方式实现此目的
在App委托中创建一个宏,以便在整个Project中使用它。它将基于设备的屏幕高度。
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
现在你可以测试iphone 4和iphone 5的条件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
if(IS_IPHONE_5){
self.window.RootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPhone5" bundle:nil];
}
else
{
self.window.RootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
}
}
}
答案 3 :(得分:1)
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone 4 Classic
//first always target 4 for better performance & practice.
}
if(result.height == 568)
{
// iPhone 5
}
}else {
//it's ipad
}