我的iOS应用为UINavigationBar
使用了一个自定义高度,这在新的iPhone X上导致了一些问题。
是否有人已经知道如何可靠地程序化地(在Objective-C中)检测一个应用程序是否在iPhone X上运行?
编辑:
当然,检查屏幕的大小是可能的,但是,我想知道是否有一些"内置"方法,如TARGET_OS_IPHONE
来检测iOS...
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height == 812)
NSLog(@"iPhone X");
}
编辑2:
我不认为我的问题与链接的问题是重复的。当然,有一些方法可以测量当前设备的不同属性,并使用这些结果来决定使用哪个设备。然而,这并不是我的问题的实际要点,我在第一次编辑中试图强调。
实际的问题是。"是否有可能直接检测出当前设备是否是iPhone X(例如通过一些SDK功能),或者我必须使用间接测量"?
根据目前给出的答案,我认为答案是"不,没有直接的方法。测量才是正途"。
根据你的问题,答案是否定的。没有直接的方法。欲了解更多信息,你可以在这里获得信息。
和
iPhone X的高度是2436 px
从【设备屏幕尺寸和分辨率】(https://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions)。
Swift 3和更高版本。
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
case 1136:
print("iPhone 5 or 5S or 5C")
case 1334:
print("iPhone 6/6S/7/8")
case 1920, 2208:
print("iPhone 6+/6S+/7+/8+")
case 2436:
print("iPhone X/XS/11 Pro")
case 2688:
print("iPhone XS Max/11 Pro Max")
case 1792:
print("iPhone XR/ 11 ")
default:
print("Unknown")
}
}
Objective-C:
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
switch ((int)[[UIScreen mainScreen] nativeBounds].size.height) {
case 1136:
printf("iPhone 5 or 5S or 5C");
break;
case 1334:
printf("iPhone 6/6S/7/8");
break;
case 1920, 2208:
printf("iPhone 6+/6S+/7+/8+");
break;
case 2436:
print("iPhone X/XS/11 Pro");
break;
case 2688:
print("iPhone XS Max/11 Pro Max");
break;
case 1792:
print("iPhone XR/ 11 ");
break;
default:
printf("Unknown");
break;
}
}
Xamarin.iOS:
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) {
if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1136) {
Console.WriteLine("iPhone 5 or 5S or 5C");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1334) {
Console.WriteLine("iPhone 6/6S/7/8");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1920 || (UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2208) {
Console.WriteLine("iPhone 6+/6S+/7+/8+");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2436) {
Console.WriteLine("iPhone X, XS, 11 Pro");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2688) {
Console.WriteLine("iPhone XS Max, 11 Pro Max");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1792) {
Console.WriteLine("iPhone XR, 11");
} else {
Console.WriteLine("Unknown");
}
}
根据你的问题,如下。
或者使用screenSize.height
作为浮点数812.0f
而不是int812
。
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
// 812.0 on iPhone X, XS
// 896.0 on iPhone XS Max, XR.
if (screenSize.height >= 812.0f)
NSLog(@"iPhone X");
}
更多信息,你可以参考iOS人机界面指南的以下页面。
Swift。
用 "topNotch "检测。
如果有人考虑使用notch来检测iPhoneX,请注意在"landscape"上,它对所有iPhone都是一样的。
var hasTopNotch: Bool {
if #available(iOS 11.0, *) {
return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
}
return false
}
目标-C。
- (BOOL)hasTopNotch {
if (@available(iOS 11.0, *)) {
return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0;
}
return NO;
}
更新。
不要使用`userInterfaceIdiom'属性来识别设备类型,正如userInterfaceIdiom的文档所解释的。
对于通用应用程序,你可以使用这个属性来为特定类型的设备定制你的应用程序的行为。例如,iPhone和iPad设备有不同的屏幕尺寸,所以你可能想根据当前设备的类型来创建不同的视图和控件。
也就是说,这个属性只是用来识别运行中的应用程序的视图样式。然而,iPhone应用程序(不是通用的)可以通过App store安装在iPad设备上,在这种情况下,userInterfaceIdiom
也会返回UIUserInterfaceIdiomPhone
。
正确的方法是通过uname
获得机器名称。详情请看下面的内容。
是的,这是有可能的。 下载UIDevice-Hardware扩展(或通过CocoaPod 'UIDevice-Hardware'安装),然后使用。
NSString* modelID = [[[UIDevice currentDevice] modelIdentifier];
BOOL isIphoneX = [modelID isEqualToString:@"iPhone10,3"] || [modelID isEqualToString:@"iPhone10,6"];
注意,这在模拟器中不会起作用,只能在实际设备上起作用。