【问题标题】:Defining greater than for a iPhone 6 screen size?定义大于 iPhone 6 的屏幕尺寸?
【发布时间】:2014-11-13 06:30:04
【问题描述】:

我使用的代码会根据设备显示某个 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 加尺寸等?

【问题讨论】:

  • 为什么会有多个 xib?只需将一个用于所有设备(可能是第二个用于 iPad)。使用自动布局和约束来正确调整大小和位置。
  • 我最初并没有编写代码,只是希望修复它,以便它适用于那些拥有该应用程序并使用 iPhone 6/6 Plus 的人,我可以担心之后改进代码,但是这是一个更紧急的修复...感谢谁也标记了这个答案...
  • 那你的问题是什么?你想为 iPhone 6 使用 iPhone 5 xib 还是你有另一个特定于 iPhone 6 的 xib?
  • 确实,如果用户有 6 或 6 plus,我只想(暂时)使用 iPhone 5 xib。
  • 如果您的应用程序(无论如何现在)不原生支持 iPhone 6,那么就没有什么可做的了。确保您没有添加任何 iPhone 6 启动图像或新的 iOS 8“启动屏幕文件”,并且您的应用程序在 iPhone 6 或 6+ 上运行时会认为它在 iPhone 5 上运行(大小方面)。您不需要任何额外的代码。

标签: ios objective-c iphone


【解决方案1】:

您可能应该像 Maddy 所说的那样使用自动布局,但是如果您想要拥有适用于特定设备的代码,那么您可能需要实现这个实用程序方法。然后,您可以根据需要创建任意数量的 xib,并将它们定位到特定设备。例如

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

我把这个方法放在一个实用程序类中。

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

【讨论】:

  • 为什么要在这里使用字符串?一个枚举就可以了。
  • @StilesCrisis,我没有接受过正式的语言培训,所以我的知识库中有很大的漏洞。我对枚举以及如何使用它们的知识很少。
【解决方案2】:

我对枚举进行了一些研究,它们很好。它们使代码更具可读性,但大多数情况下它们允许编译器帮助您键入和捕获错误。 Xcode 会为你自动完成你的 deviceType 并且会给你错误:Use of undeclared identifier 如果你尝试使用一个未定义的值。这是重写为枚举的代码。我在值前面加上了 LF,但您应该使用适合您项目的任何值。

这是在我的头文件中

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

这是在我的 .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;
}

这样称呼它:

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

【讨论】:

    猜你喜欢
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    相关资源
    最近更新 更多