簡體   English   中英

定義大於iPhone 6屏幕尺寸?

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

我使用的代碼根據設備顯示特定的xib文件,目前有3個xib,一個用於iPhone4,一個用於iPhone5,一個用於iPad。

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

如果我將568的值更改為更高,它將可以工作,但是有沒有辦法將其組合起來,例如,身高是568或iPhone6尺寸還是iPhone 6加尺寸等?

您可能應該像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;
}

我對枚舉做了一些研究,它們很好。 它們使代碼更具可讀性,但是大多數情況下,它們使編譯器可以幫助您鍵入和捕獲錯誤。 Xcode會自動為您完成deviceType並給您錯誤:如果嘗試使用未定義的值, 請使用未聲明的標識符 這是重寫為枚舉的代碼。 我為這些值加上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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM