如何检测用户的 iPhone 6 Plus 是否处于标准模式或缩放模式?这可能吗?
我已经尝试过[UIScreen mainScreen].scale
,它3.0
在两种情况下都会报告。
如何检测用户的 iPhone 6 Plus 是否处于标准模式或缩放模式?这可能吗?
我已经尝试过[UIScreen mainScreen].scale
,它3.0
在两种情况下都会报告。
有一个新成员
[[UIScreen mainScreen] nativeScale]
这应该做你想做的事。它仅在 iOS 8 上可用,因此您需要保护它
[UIScreen mainScreen].currentMode
报告:
<UIScreenMode: 0x17802f240; size = 1242.000000 x 2208.000000> // STANDARD
<UIScreenMode: 0x178226be0; size = 1125.000000 x 2001.000000> // ZOOMED
以下代码可用于获取bounds
、和coordinateSpace
,即在 iPhone 6 Plus 上为 2.608,当设备在缩放模式下运行时为 2.88(注意在模拟器中不同):nativeScale
scale
nativeScale
UIScreen *mainScreen = [UIScreen mainScreen];
NSLog(@"Screen bounds: %@, Screen resolution: %@, scale: %f, nativeScale: %f",
NSStringFromCGRect(mainScreen.bounds), mainScreen.coordinateSpace, mainScreen.scale, mainScreen.nativeScale);
检测 iPhone 6 Plus 的代码:
-(BOOL)iPhone6PlusDevice{
// Scale is 3 currently only for iPhone 6 Plus
if ([UIScreen mainScreen].scale > 2.9) return YES;
return NO;
}
或者
-(BOOL)iPhone6PlusUnZoomed{
if ([self iPhone6PlusDevice]){
if ([UIScreen mainScreen].bounds.size.height > 720.0) return YES; // Height is 736, but 667 when zoomed.
}
return NO;
}
注意:如果您正在检查 iPhone 6 Plus,要调整用户界面,请不要依赖.nativeScale
,因为模拟器和实际设备会给出不同的结果。
Paula Chavarría对 iOS 8的回答[UIScreen mainScreen].bounds.size
更新了宏(取决于方向):
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && (MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 568.0) && ((IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale) || !IS_OS_8_OR_LATER))
#define IS_STANDARD_IPHONE_6 (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale)
#define IS_ZOOMED_IPHONE_6 (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 568.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale > [UIScreen mainScreen].scale)
#define IS_STANDARD_IPHONE_6_PLUS (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 736.0)
#define IS_ZOOMED_IPHONE_6_PLUS (IS_IPHONE && MAX([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width) == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale < [UIScreen mainScreen].scale)
#define IS_IPHONE_6 (IS_STANDARD_IPHONE_6 || IS_ZOOMED_IPHONE_6)
#define IS_IPHONE_6_PLUS (IS_STANDARD_IPHONE_6_PLUS || IS_ZOOMED_IPHONE_6_PLUS)
这是一个简单的解决方案:
旧解决方案(在某些极端情况下不可靠):
var isZoomed: Bool { return UIScreen.main.scale != UIScreen.main.nativeScale }
更新:
//Display Zoom mode
var isZoomed: Bool {
return UIScreen.main.scale < UIScreen.main.nativeScale
}
PS:请不要混淆此功能:
Settings -> Display & Brightness -> Display Zoom
与:
Settings -> Accessibility -> Zoom
。
这些选项用于检测 iPhone 设备。
#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_6PLUS (IS_IPHONE && [[UIScreen mainScreen] nativeScale] == 3.0f)
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
#define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0)