2014-10-16 2 views
1

장치에 따라 특정 xib 파일을 표시하는 코드를 사용하고 있습니다. 현재 xibs 3 개, iPhone4 용, iPhone5 용 및 iPad 용 중 하나입니다.iPhone 6 화면 크기보다 더 큰 값을 정의 하시겠습니까?

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 ​​크기 또는 아이폰 6 플러스 크기 등이다 결합하는 방법은 무엇입니까?

+0

이유는 여러 xibs을해야합니까? 모든 기기에 하나만 사용하십시오 (iPad의 경우 2 번째). 자동 레이아웃과 구속 조건을 사용하여 모든 크기와 위치를 적절히 맞 춥니 다. – rmaddy

+1

나는 코드를 원래 작성하지 않았으며, 단지 아이폰 6/6 Plus를 사용하는 사람들을 위해 작동하도록 수정했다. 나중에 코드를 수정하는 것에 대해 걱정할 수는 있지만, 이것은 더 시급한 수정이다. .. 이 답변을 표시 한 사람 덕분에 ... – user3355723

+0

다음 질문은 무엇입니까? iPhone 6 용 iPhone 5 xib를 사용 하시겠습니까? 아니면 iPhone 6 전용의 다른 xib가 있습니까? – rmaddy

답변

1

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

왜 여기에 문자열을 사용합니까? enum이 할 것입니다. – StilesCrisis

+0

@StilesCrisis, 공식적인 언어 교육이 없기 때문에 지식 기반에 큰 구멍이 있습니다. 열거 형에 대한 나의 지식과 그것들을 사용하는 방법은 최소한이다. – JScarry

0

저는 열거 형에 대한 연구를했는데 훌륭합니다. 코드를 좀 더 읽기 쉽도록 만들지 만 대부분 컴파일러가 오류를 입력하고 catch하는 데 도움을줍니다. Xcode는 deviceType을 자동 완성하여 다음과 같은 오류 메시지를 표시합니다 : 정의되지 않은 값을 사용하려고하면 알 려진 식별자을 사용합니다. 다음은 enum으로 다시 작성된 코드입니다. 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; 
} 
관련 문제