我正在使用根据设备显示特定 xib 文件的代码,目前有 3 个 xib,一个用于 iPhone4,一个用于 iPhone5,一个用于 iPad。
自 iOS8/iPhone 6 以来,由于 xib 不知道要加载什么,该应用程序无法在高于 5s 的任何设备上运行;
- (IBAction)btnTapForStart:(id)sender {
fromOtherClass=YES;
if([[UIScreen mainScreen]bounds].size.height == 568)
{
PlayMusicViewController *pmvc = [[PlayMusicViewController alloc]initWithNibName:@"XibForIPhone5" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:pmvc animated:YES];
}
else
{
PlayMusicViewController *pmvc = [[PlayMusicViewController alloc]init];
[self.navigationController pushViewController:pmvc animated:YES];
}
[appObject.audioPlayer stop];
}
如果我将 568 的值更改为更高的值,它会起作用,但是有没有办法组合它,例如,高度是 568 或 iPhone6 尺寸或 iPhone 6 加大尺寸等?
最佳答案
您可能应该像 Maddy 所说的那样使用自动布局,但是如果您希望拥有适用于特定设备的代码,那么您可能希望实现此实用程序方法。然后,您可以根据需要创建任意数量的 xib,并将它们定位到特定设备。例如
if ([[Utilities deviceType] isEqualToString:@"iPhone Retina4"] || [[Utilities deviceType] isEqualToString:@"iPhone Retina35"] ) {
-- do something specific for these phones
}
我把这个方法放在 Utilities 类中。
+ (NSString *)deviceType {
NSString *device = nil;
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat deviceScale = [UIScreen mainScreen].scale;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
device = @"iPhone Classic"; // Just in case it doesn't make it through the conditionals
// Classic has a resolution of 480 × 320
if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 1.0f ) {
device = @"iPhone Classic";
// Retina has a resolution of 960 × 640
} else if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 2.0f ) {
device = @"iPhone Retina35";
// Retina 4" has a resolution of 1136 x 640
} else if (screenSize.height == 568 || screenSize.width == 568 ) {
device = @"iPhone Retina4";
// iPhone 6 has a resolution of 1334 by 750
} else if (screenSize.height == 667 || screenSize.width == 667 ) {
device = @"iPhone 6";
// iPhone 6 Plus has an actual size of 2208 × 1242 and resolution of 1920 by 1080
// Reported size is 736 x 414
} else if (screenSize.height == 736 || screenSize.width == 736 ) {
device = @"iPhone 6 Plus";
}
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
device = @"iPad Classic"; // Just in case it doesn't make it through the conditionals
if(deviceScale == 1.0f) {
device = @"iPad Classic";
} else if (deviceScale == 2.0f) {
device = @"iPad Retina";
}
}
//NSLog(@"The device is %@ scale is %f and the height is %f and width is %f", device, deviceScale, screenSize.height, screenSize.width);
return device;
}
关于ios - 定义大于 iPhone 6 屏幕尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26408568/