简体   繁体   English

定义大于iPhone 6屏幕尺寸?

[英]Defining greater than for a iPhone 6 screen size?

I am using code which shows a certain xib file depending on the device, at the moment 3 xibs, one for iPhone4, one for iPhone5 and one for iPad. 我使用的代码根据设备显示特定的xib文件,目前有3个xib,一个用于iPhone4,一个用于iPhone5,一个用于iPad。

Since iOS8 / iPhone 6, the app does not work on anything higher than the 5s due to the fact the xib does not know what to load; 从iOS8 / iPhone 6开始,该应用无法在高于5s的版本上运行,原因是xib不知道要加载什么。

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

If I change the value of 568 to higher it will work, but is there a way to combine it, for example, height is 568 OR iPhone6 size OR iPhone 6 plus size etc? 如果我将568的值更改为更高,它将可以工作,但是有没有办法将其组合起来,例如,身高是568或iPhone6尺寸还是iPhone 6加尺寸等?

You probably should use auto-layout like Maddy says, but if you want to have code that works for specific devices, then you might want to implement this utility method. 您可能应该像Maddy所说的那样使用自动布局,但是如果您想拥有适用于特定设备的代码,那么您可能想要实现此实用程序方法。 Then you can create as many xibs as you want and target them to specific devices. 然后,您可以根据需要创建任意数量的Xib,并将它们定位到特定设备。 eg 例如

if ([[Utilities deviceType] isEqualToString:@"iPhone Retina4"] || [[Utilities deviceType] isEqualToString:@"iPhone Retina35"] ) {
   -- do something specific for these phones
}

I put this method in a Utilities class. 我将此方法放在实用程序类中。

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

I did some research on enums and they are nice. 我对枚举做了一些研究,它们很好。 They make the code a bit more readable, but mostly they allow the compiler to help you type and catch errors. 它们使代码更具可读性,但是大多数情况下,它们使编译器可以帮助您键入和捕获错误。 Xcode will autocomplete your deviceType for you and will give you the error: Use of undeclared identifier if you try to use a value that isn't defined. Xcode会自动为您完成deviceType并给您错误:如果尝试使用未定义的值, 请使用未声明的标识符 Here's the code rewritten as an enum. 这是重写为枚举的代码。 I prefixed the values with LF but you should use whatever is appropriate for your project. 我为这些值加上LF前缀,但您应该使用适合您项目的任何值。

This is in my header file 这是在我的头文件中

// Devices as of Fall 2014
typedef NS_ENUM(NSInteger, LFdeviceType) {
    LFDeviceTypePhoneClassic,
    LFDeviceTypePhoneRetina3_5,
    LFDeviceTypePhoneRetina4,
    LFDeviceTypePhone6,
    LFDeviceTypePhone6Plus,
    LFDeviceTypePadClassic,
    LFDeviceTypePadRetina,
};

And this is in my .m file. 这是在我的.m文件中。

m+ (NSInteger)deviceType {
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    CGFloat deviceScale = [UIScreen mainScreen].scale;
    LFdeviceType device = LFDeviceTypePhoneClassic;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        device = LFDeviceTypePhoneClassic; // 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 = LFDeviceTypePhoneClassic;
        // Retina has a resolution of 960 × 640
        } else if( (screenSize.height == 480 || screenSize.width == 480) && deviceScale == 2.0f ) {
            device = LFDeviceTypePhoneRetina3_5;
        // Retina 4" has a resolution of 1136 x 640
        } else if (screenSize.height == 568 || screenSize.width == 568 ) {
            device = LFDeviceTypePhoneRetina4;
        // iPhone 6 has a resolution of 1334 by 750
        } else if (screenSize.height == 667 || screenSize.width == 667 ) {
            device = LFDeviceTypePhone6;
        // 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 = LFDeviceTypePhone6Plus;
        }

    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        device = LFDeviceTypePadClassic; // Just in case it doesn't make it through the conditionals
        if(deviceScale == 1.0f) {
            device = LFDeviceTypePadClassic;
        } else if (deviceScale == 2.0f) {
            device = LFDeviceTypePadRetina;
        }
    }
    //NSLog(@"The device is %@ scale is %f and the height is %f and width is %f", device, deviceScale, screenSize.height, screenSize.width);

    return device;
}

Call it like this: 这样称呼它:

if ( (   [Utilities deviceType] == LFDeviceTypePhoneClassic 
      || [Utilities deviceType] == LFDeviceTypePhoneRetina3_5) &&
        numberOfFoilsOnScreen > 7 ) {
        numberOfFoilsOnScreen = 7;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM