아이폰 5(와이드스크린 장치)를 감지하는 방법은 무엇입니까?
저는 방금 Xcode 4.5 GM으로 업그레이드했고 이제 스토리보드의 뷰 컨트롤러에 '4" Retina' 크기를 적용할 수 있다는 것을 알게 되었습니다.
이제 제가 아이폰 4와 5에서 모두 실행되는 애플리케이션을 만들려면 물론 모든 창을 두 번씩 빌드해야 하지만 사용자가 3.5인치 또는 4인치 화면을 가진 아이폰을 가지고 있는지 감지하고 뷰를 적용해야 합니다.
어떻게 해야 하나요?
먼저 모든 보기를 새 화면에 맞게 재구성하거나 화면 크기에 따라 다른 보기를 사용해서는 안 됩니다.
iOS의 자동 크기 조정 기능을 사용하여 화면 크기를 조정하고 조정할 수 있습니다.
그렇게 어렵지는 않습니다. 문서를 좀 읽어 보십시오.그것은 당신에게 많은 시간을 절약해 줄 것입니다.
이에 합니다.OS 6는 이에 대한 새로운 기능도 제공합니다.
Apple Developer 웹 사이트에서 iOS 6 API 변경 로그를 반드시 읽어보십시오.
그리고 새로운 iOS 6 AutoLayout 기능을 확인합니다.
즉, 만약 당신이 정말로 아이폰 5를 감지해야 한다면, 당신은 단순히 화면 크기에 의존할 수 있습니다.
[ [ UIScreen mainScreen ] bounds ].size.height
아이폰5의 화면 높이는 568입니다.
모든매크로를 할 수 : 모 든 것 을 단 하 매 수 있 상 니 습 다 할 상 이 를 로 크 해 위 기 화 순 다 ▁you 니 있 습 , 이 수 ▁a ▁macro ▁simplify▁to ▁all
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
의 fabs
H2CO3의 코멘트에서 지적한 바와 같이 부동 소수점을 비교할 때 정밀 오류를 방지하기 위해 엡실론을 사용합니다.
이제부터 표준 if/else 문에서 사용할 수 있습니다.
if( IS_IPHONE_5 )
{}
else
{}
편집 - 더 나은 탐지 기능
일부 사람들이 말한 것처럼, 이것은 실제 아이폰 5가 아닌 와이드 스크린만 감지합니다.
iPod touch의 다음 버전에도 이러한 화면이 있을 수 있으므로 다른 매크로 세트를 사용할 수 있습니다.
매크로의 해 보겠습니다.IS_WIDESCREEN
:
#define IS_WIDESCREEN ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
모델 탐지 매크로를 추가해 보겠습니다.
#define IS_IPHONE ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPhone" ] )
#define IS_IPOD ( [ [ [ UIDevice currentDevice ] model ] isEqualToString: @"iPod touch" ] )
이러한 방식으로, 우리는 아이폰 모델과 와이드스크린을 확보할 수 있고, 우리는 다시 정의할 수 있습니다.IS_IPHONE_5
설정:
#define IS_IPHONE_5 ( IS_IPHONE && IS_WIDESCREEN )
또한 @LearnCocos2D에서 언급한 바와 같이 애플리케이션이 iPhone 5 화면에 최적화되지 않은 경우(Default-568h@2x.pn g 이미지 표시) 이 매크로는 작동하지 않습니다. 이러한 경우 화면 크기는 여전히 320x1200입니다.
최적화되지 않은 앱에서 아이폰 5를 감지하려는 이유를 알 수 없기 때문에 이것이 문제가 될 수 있다고 생각하지 않습니다.
중요 - iOS 8 지원
8에서 8은bounds
의 UIScreen
클래스는 이제 장치 방향을 반영합니다.
따라서 이전 코드는 즉시 사용할 수 없습니다.
하기 위해, 이문를해간수있사다습니용할새기를 .nativeBounds
성속 bounds
방향에 따라 변경되지 않고 세로로 위로 이동하는 모드를 기반으로 하기 때문입니다.
로 참로치수의 nativeBounds
픽셀 단위로 측정되므로 아이폰 5의 경우 높이는 568이 아닌 1136입니다.
또한 iOS 7 이하를 대상으로 하는 경우에는 기능 감지를 사용하여 호출해야 합니다.nativeBounds
8 버전에서는합니다. iOS 8 버전에서는 앱이 충돌합니다.
if( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] )
{
/* Detect using nativeBounds - iOS 8 and greater */
}
else
{
/* Detect using bounds - iOS 7 and lower */
}
다음과 같은 방법으로 이전 매크로를 조정할 수 있습니다.
#define IS_WIDESCREEN_IOS7 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define IS_WIDESCREEN_IOS8 ( fabs( ( double )[ [ UIScreen mainScreen ] nativeBounds ].size.height - ( double )1136 ) < DBL_EPSILON )
#define IS_WIDESCREEN ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_WIDESCREEN_IOS8 : IS_WIDESCREEN_IOS7 )
그리고 아이폰 6 또는 6 Plus를 감지해야 하는 경우 해당 화면 크기를 사용하십시오.
SDK와 OS의 조합을 위해 테스트 및 설계되었습니다.
스위프트
iPad 유형 추가. iPad 2 및 iPad mini는 비레티나 iPad입니다.iPad Mini 2 이상에서는 iPad 3, 4, iPad Air, Air 2, Air 3 및 iPad Pro 9.7의 논리 해상도가 1024이고, iPad Pro의 최대 길이는 1366입니다. 언급
import UIKit
public enum DisplayType {
case unknown
case iphone4
case iphone5
case iphone6
case iphone6plus
case iPadNonRetina
case iPad
case iPadProBig
static let iphone7 = iphone6
static let iphone7plus = iphone6plus
}
public final class Display {
class var width:CGFloat { return UIScreen.main.bounds.size.width }
class var height:CGFloat { return UIScreen.main.bounds.size.height }
class var maxLength:CGFloat { return max(width, height) }
class var minLength:CGFloat { return min(width, height) }
class var zoomed:Bool { return UIScreen.main.nativeScale >= UIScreen.main.scale }
class var retina:Bool { return UIScreen.main.scale >= 2.0 }
class var phone:Bool { return UIDevice.current.userInterfaceIdiom == .phone }
class var pad:Bool { return UIDevice.current.userInterfaceIdiom == .pad }
class var carplay:Bool { return UIDevice.current.userInterfaceIdiom == .carPlay }
class var tv:Bool { return UIDevice.current.userInterfaceIdiom == .tv }
class var typeIsLike:DisplayType {
if phone && maxLength < 568 {
return .iphone4
}
else if phone && maxLength == 568 {
return .iphone5
}
else if phone && maxLength == 667 {
return .iphone6
}
else if phone && maxLength == 736 {
return .iphone6plus
}
else if pad && !retina {
return .iPadNonRetina
}
else if pad && retina && maxLength == 1024 {
return .iPad
}
else if pad && maxLength == 1366 {
return .iPadProBig
}
return .unknown
}
}
https://gist.github.com/hfossli/bc93d924649de881ee2882457f14e346 에서 확인할 수 있습니다.
참고: 예를 들어 iPhone 6이 확대 모드인 경우 UI는 iPhone 5의 확대 버전입니다.이러한 기능은 장치 유형을 결정하는 것이 아니라 디스플레이 모드이므로 iPhone 5가 이 예에서 원하는 결과입니다.
목표-C
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_ZOOMED (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
사용량: http://pastie.org/9687735
참고: 예를 들어 iPhone 6이 확대 모드인 경우 UI는 iPhone 5의 확대 버전입니다.이러한 기능은 장치 유형을 결정하는 것이 아니라 디스플레이 모드이므로 iPhone 5가 이 예에서 원하는 결과입니다.
정말 간단한 솔루션
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone Classic
}
if(result.height == 568)
{
// iPhone 5
}
}
이제 iPhone 6와 6Plus 화면 크기를 고려해야 합니다.다음은 업데이트된 답변입니다.
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//its iPhone. Find out which one?
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone Classic
}
else if(result.height == 568)
{
// iPhone 5
}
else if(result.height == 667)
{
// iPhone 6
}
else if(result.height == 736)
{
// iPhone 6 Plus
}
}
else
{
//its iPad
}
유용한 정보
iPhone 6 Plus 736x414 points 2208x1242 pixels 3x scale 1920x1080 physical pixels 401 physical ppi 5.5"
iPhone 6 667x375 points 1334x750 pixels 2x scale 1334x750 physical pixels 326 physical ppi 4.7"
iPhone 5 568x320 points 1136x640 pixels 2x scale 1136x640 physical pixels 326 physical ppi 4.0"
iPhone 4 480x320 points 960x640 pixels 2x scale 960x640 physical pixels 326 physical ppi 3.5"
iPhone 3GS 480x320 points 480x320 pixels 1x scale 480x320 physical pixels 163 physical ppi 3.5"
저는 맥이 만든 매크로를 C 기능에 넣고, 아이폰 5가 아니라도 넓은 화면 가용성을 감지하기 때문에 적절하게 이름을 붙였습니다.
프로젝트에 Default-568h@2x.pn g가 포함되지 않은 경우에도 매크로는 iPhone 5에서 실행 중인 것을 감지하지 못합니다.새 기본 이미지가 없으면 iPhone 5는 일반 480x320 화면 크기(포인트 단위)를 보고합니다.따라서 검사는 와이드 스크린 가용성뿐만 아니라 와이드 스크린 모드도 활성화해야 합니다.
BOOL isWidescreenEnabled()
{
return (BOOL)(fabs((double)[UIScreen mainScreen].bounds.size.height -
(double)568) < DBL_EPSILON);
}
iPhone4, iphone5, ipad, iphone6, iphone6p용 ios7/ios8에 대한 테스트를 통과한 코드는 장치나 시뮬레이터에 관계없이 다음과 같습니다.
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) // iPhone and iPod touch style UI
#define IS_IPHONE_5_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
#define IS_IPHONE_6_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f)
#define IS_IPHONE_6P_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f)
#define IS_IPHONE_4_AND_OLDER_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height < 568.0f)
#define IS_IPHONE_5_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 568.0f)
#define IS_IPHONE_6_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 667.0f)
#define IS_IPHONE_6P_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 736.0f)
#define IS_IPHONE_4_AND_OLDER_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) < 568.0f)
#define IS_IPHONE_5 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_5_IOS8 : IS_IPHONE_5_IOS7 )
#define IS_IPHONE_6 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6_IOS8 : IS_IPHONE_6_IOS7 )
#define IS_IPHONE_6P ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6P_IOS8 : IS_IPHONE_6P_IOS7 )
#define IS_IPHONE_4_AND_OLDER ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_4_AND_OLDER_IOS8 : IS_IPHONE_4_AND_OLDER_IOS7 )
hfossli의 답변을 사용하여 Swift로 번역하였습니다.
let IS_IPAD = UIDevice.currentDevice().userInterfaceIdiom == .Pad
let IS_IPHONE = UIDevice.currentDevice().userInterfaceIdiom == .Phone
let IS_RETINA = UIScreen.mainScreen().scale >= 2.0
let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height
let SCREEN_MAX_LENGTH = max(SCREEN_WIDTH, SCREEN_HEIGHT)
let SCREEN_MIN_LENGTH = min(SCREEN_WIDTH, SCREEN_HEIGHT)
let IS_IPHONE_4_OR_LESS = (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
let IS_IPHONE_5 = (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
let IS_IPHONE_6 = (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
let IS_IPHONE_6P = (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
이것은 제 코코스 2d 프로젝트의 매크로입니다.다른 앱에서도 동일해야 합니다.
#define WIDTH_IPAD 1024
#define WIDTH_IPHONE_5 568
#define WIDTH_IPHONE_4 480
#define HEIGHT_IPAD 768
#define HEIGHT_IPHONE 320
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//width is height!
#define IS_IPHONE_5 ( [ [ UIScreen mainScreen ] bounds ].size.height == WIDTH_IPHONE_5 )
#define IS_IPHONE_4 ( [ [ UIScreen mainScreen ] bounds ].size.height == WIDTH_IPHONE_4 )
#define cp_ph4(__X__, __Y__) ccp(cx_ph4(__X__), cy_ph4(__Y__))
#define cx_ph4(__X__) (IS_IPAD ? (__X__ * WIDTH_IPAD / WIDTH_IPHONE_4) : (IS_IPHONE_5 ? (__X__ * WIDTH_IPHONE_5 / WIDTH_IPHONE_4) : (__X__)))
#define cy_ph4(__Y__) (IS_IPAD ? (__Y__ * HEIGHT_IPAD / HEIGHT_IPHONE) : (__Y__))
#define cp_pad(__X__, __Y__) ccp(cx_pad(__X__), cy_pad(__Y__))
#define cx_pad(__X__) (IS_IPAD ? (__X__) : (IS_IPHONE_5 ? (__X__ * WIDTH_IPHONE_5 / WIDTH_IPAD) : (__X__ * WIDTH_IPHONE_4 / WIDTH_IPAD)))
#define cy_pad(__Y__) (IS_IPAD ? (__Y__) : (__Y__ * HEIGHT_IPHONE / HEIGHT_IPAD))
if ((int)[[UIScreen mainScreen] bounds].size.height == 568)
{
// This is iPhone 5 screen
} else {
// This is iPhone 4 screen
}
스위프트에서 iOS 8+ 프로젝트를 확장하고 싶습니다.UIScreen
예:
extension UIScreen {
var isPhone4: Bool {
return self.nativeBounds.size.height == 960;
}
var isPhone5: Bool {
return self.nativeBounds.size.height == 1136;
}
var isPhone6: Bool {
return self.nativeBounds.size.height == 1334;
}
var isPhone6Plus: Bool {
return self.nativeBounds.size.height == 2208;
}
}
(참고:nativeBounds
픽셀 단위).
그러면 코드는 다음과 같습니다.
if UIScreen.mainScreen().isPhone4 {
// do smth on the smallest screen
}
그래서 코드는 이것이 장치 모델이 아니라 메인 화면에 대한 검사임을 분명히 합니다.
Samrat Mazumdar의 대답을 빌려, 여기 장치 화면 크기를 추정하는 간단한 방법이 있습니다.최신 장치와 함께 작동하지만 미래 장치에서는 실패할 수 있습니다(모든 추측 방법이 가능함).또한 장치가 미러링되는지 여부도 혼동됩니다(미러링된 화면 크기가 아닌 장치의 화면 크기를 반환함).
#define SCREEN_SIZE_IPHONE_CLASSIC 3.5
#define SCREEN_SIZE_IPHONE_TALL 4.0
#define SCREEN_SIZE_IPAD_CLASSIC 9.7
+ (CGFloat)screenPhysicalSize
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if (result.height < 500)
return SCREEN_SIZE_IPHONE_CLASSIC; // iPhone 4S / 4th Gen iPod Touch or earlier
else
return SCREEN_SIZE_IPHONE_TALL; // iPhone 5
}
else
{
return SCREEN_SIZE_IPAD_CLASSIC; // iPad
}
}
이 매크로가 장치와 시뮬레이터에서 작동한다면 좋을 것 같습니다, 아래의 해결책이 있습니다.
#define IS_WIDESCREEN (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)
#define IS_IPHONE (([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"]) || ([[[UIDevice currentDevice] model] isEqualToString: @"iPhone Simulator"]))
#define IS_IPOD ([[[UIDevice currentDevice]model] isEqualToString:@"iPod touch"])
#define IS_IPHONE_5 ((IS_IPHONE || IS_IPOD) && IS_WIDESCREEN)
답변에 시뮬레이터에 대한 특별한 사례가 포함되어 있지 않다는 것을 알게 되었습니다.
#define IS_WIDESCREEN ( [ [ UIScreen mainScreen ] bounds ].size.height == 568 )
#define IS_IPHONE ([[ [ UIDevice currentDevice ] model ] rangeOfString:@"iPhone"].location != NSNotFound)
#define IS_IPAD ([[ [ UIDevice currentDevice ] model ] rangeOfString:@"iPad"].location != NSNotFound)
#define IS_IPHONE_5 ( IS_IPHONE && IS_WIDESCREEN )
+(BOOL)isDeviceiPhone5
{
BOOL iPhone5 = FALSE;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
// code for 4-inch screen
iPhone5 = TRUE;
}
else
{
iPhone5 = FALSE;
// code for 3.5-inch screen
}
return iPhone5;
}
이에 대해 수백 번의 답변이 있었지만, 이 솔루션은 저에게 가장 효과적이었고 새 장치가 도입되고 크기가 정의되지 않은 경우 문제를 해결하는 데 도움이 되었습니다.
Swift 5 도우미:
extension UIScreen {
func phoneSizeInInches() -> CGFloat {
switch (self.nativeBounds.size.height) {
case 960, 480:
return 3.5 //iPhone 4
case 1136:
return 4 //iPhone 5
case 1334:
return 4.7 //iPhone 6
case 2208:
return 5.5 //iPhone 6 Plus
case 2436:
return 5.8 //iPhone X
case 1792:
return 6.1 //iPhone XR
case 2688:
return 6.5 //iPhone XS Max
default:
let scale = self.scale
let ppi = scale * 163
let width = self.bounds.size.width * scale
let height = self.bounds.size.height * scale
let horizontal = width / ppi, vertical = height / ppi
let diagonal = sqrt(pow(horizontal, 2) + pow(vertical, 2))
return diagonal
}
}
}
이것은 "5.5인치" 또는 "4.7인치" 장치와 같은 전화기의 인치 크기를 기억하기는 쉽지만 정확한 픽셀 크기를 기억하기는 어렵기 때문입니다.
if UIScreen.main.phoneSizeInInches() == 4 {
//do something with only 4 inch iPhones
}
이를 통해 다음과 같은 작업을 수행할 수 있습니다.
if UIScreen.main.phoneSizeInInches() < 5.5 {
//do something on all iPhones smaller than the plus
}
기본값은 화면 크기와 스케일을 사용하여 대각선 인치를 계산합니다.이는 새 장치 크기가 나타나는 경우 마지막 예제와 같은 코드가 계속 작동하는지 확인하기 위해 최선을 다합니다.
CGFloat height = [UIScreen mainScreen].bounds.size.height;
NSLog(@"screen soze is %f",height);
if (height>550) {
// 4" screen-do some thing
}
else if (height<500) {
// 3.5 " screen- do some thing
}
크기에 의존하는 것은 많은 수준에서 잘못된 것입니다.우리가 시스템에 요청하는 것은 어떻습니까?
- (NSString *) getDeviceModel
{
struct utsname systemInfo;
uname(&systemInfo);
return [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
}
iPhone4 또는 iPhone5 하드웨어 유형을 탐지하는 최상의 방법에서 가져온 것으로 edzio27 답변.
이렇게 하면 장치 패밀리를 탐지할 수 있습니다.
#import <sys/utsname.h>
NSString* deviceName()
{
struct utsname systemInformation;
uname(&systemInformation);
NSString *result = [NSString stringWithCString:systemInformation.machine
encoding:NSUTF8StringEncoding];
return result;
}
#define isIPhone5 [deviceName() rangeOfString:@"iPhone5,"].location != NSNotFound
#define isIPhone5S [deviceName() rangeOfString:@"iPhone6,"].location != NSNotFound
Xcode 6을 사용하여 프로젝트를 만든 경우 아래에 언급된 코드를 사용하여 장치를 탐지합니다.
printf("\nDetected Resolution : %d x %d\n\n",(int)[[UIScreen mainScreen] nativeBounds].size.width,(int)[[UIScreen mainScreen] nativeBounds].size.height);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
{
if([[UIScreen mainScreen] nativeBounds].size.height == 960 || [[UIScreen mainScreen] nativeBounds].size.height == 480){
printf("Device Type : iPhone 4,4s ");
}else if([[UIScreen mainScreen] nativeBounds].size.height == 1136){
printf("Device Type : iPhone 5,5S/iPod 5 ");
}else if([[UIScreen mainScreen] nativeBounds].size.height == 1334){
printf("Device Type : iPhone 6 ");
}else if([[UIScreen mainScreen] nativeBounds].size.height == 2208){
printf("Device Type : iPhone 6+ ");
}
}
}else{
printf("Device Type : iPad");
}
프로젝트가 Xcode 5에서 생성되고 Xcode 6에서 열린 경우 아래에 언급된 코드를 사용하여 장치를 탐지합니다.(이 코드는 iPhone 6,6+에 대한 실행 이미지가 할당되지 않은 경우 작동합니다.)
printf("\nDetected Resolution : %d x %d\n\n",(int)[[UIScreen mainScreen] nativeBounds].size.width,(int)[[UIScreen mainScreen] nativeBounds].size.height);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
{
if([[UIScreen mainScreen] nativeBounds].size.height == 960 || [[UIScreen mainScreen] nativeBounds].size.height == 480){
printf("Device Type : iPhone 4,4s");
appType=1;
}else if([[UIScreen mainScreen] nativeBounds].size.height == 1136 || [[UIScreen mainScreen] nativeBounds].size.height == 1704){
printf("Device Type : iPhone 5,5S,6,6S/iPod 5 ");
appType=3;
}
}
}else{
printf("Device Type : iPad");
appType=2;
}
여전히 Xcode 5를 함께 사용하는 경우 다음 코드를 사용하여 장치를 감지합니다(iPhone 6 및 6+는 감지되지 않음).
printf("\nDetected Resolution : %d x %d\n\n",(int)[[UIScreen mainScreen] bounds].size.width,(int)[[UIScreen mainScreen] bounds].size.height);
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
{
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 960 || result.height == 480){
printf("Device Type : iPhone 4,4S ");
}else if(result.height == 1136){
printf("Device Type : iPhone 5s/iPod 5");
}
}
}else{
printf("Device Type : iPad");
}
File'> 'swift 일파추' ->
AppDelegateEx.swift
에 확장 기능을 추가합니다.
AppDelegate
import UIKit extension AppDelegate { class func isIPhone5 () -> Bool{ return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 568.0 } class func isIPhone6 () -> Bool { return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 667.0 } class func isIPhone6Plus () -> Bool { return max(UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height) == 736.0 } }
용도:
if AppDelegate.isIPhone5() { collectionViewTopConstraint.constant = 2 }else if AppDelegate.isIPhone6() { collectionViewTopConstraint.constant = 20 }
스위프트 3에서는 간단한 클래스 KRDeviceType을 사용할 수 있습니다.
https://github.com/ulian-onua/KRDeviceType
연산자 ==, >=, <=>를 잘 문서화하고 지원합니다.
예를 들어 장치에 iPhone 6/6s/7의 범위가 있는지 감지하려면 다음 비교를 사용합니다.
if KRDeviceType() == .iPhone6 {
// Perform appropiate operations
}
장치에 iPhone 5/5S/SE 또는 이전 버전(iPhone 4s)의 범위가 있는지 감지하려면 다음 비교를 사용합니다.
if KRDeviceType() <= .iPhone5 { //iPhone 5/5s/SE of iPhone 4s
// Perform appropiate operations (for example, set up constraints for those old devices)
}
다음 코드를 사용합니다.
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
if (screenSize.height==1136.000000)
{
// Here iPhone 5 View
// Eg: Nextview~iPhone5.Xib
} else {
// Previous Phones
// Eg : Nextview.xib
}
다음은 방향에 관계없이 올바른 장치 테스트입니다.
- (BOOL)isIPhone5
{
CGSize size = [[UIScreen mainScreen] bounds].size;
if (MIN(size.width,size.height) == 320 && MAX(size.width,size.height == 568)) {
return YES;
}
return NO;
}
모든 버전의 iPhone 및 iPad 장치를 탐지하는 데 사용됩니다.
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
#define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0)
언급URL : https://stackoverflow.com/questions/12446990/how-to-detect-iphone-5-widescreen-devices
'itsource' 카테고리의 다른 글
fatal: not git 저장소(또는 상위 디렉터리): .git (0) | 2023.05.27 |
---|---|
"npm install"이 package-lock.json을 다시 쓰는 이유는 무엇입니까? (0) | 2023.05.27 |
MongoDB에서 다대다 관계를 구성하는 방법 (0) | 2023.05.27 |
VBA를 사용하여 폴더의 Excel 파일 목록 가져오기 (0) | 2023.05.27 |
잘못된 병합을 수정하고 수정된 병합에 좋은 커밋을 재생하려면 어떻게 해야 합니까? (0) | 2023.05.27 |