【发布时间】:2014-04-17 08:55:03
【问题描述】:
我对 iphone 4/4S 和 iphone 5/5s/5c 有两种不同的看法。我需要根据手机型号显示正确的视图。 有人可以告诉我您如何编写应用程序来检查手机型号,然后显示 3.5 或 4 英寸视图吗?非常感谢!
【问题讨论】:
-
自动布局可以解决您的问题吗?
-
@Larme 如何开启自动布局?
我对 iphone 4/4S 和 iphone 5/5s/5c 有两种不同的看法。我需要根据手机型号显示正确的视图。 有人可以告诉我您如何编写应用程序来检查手机型号,然后显示 3.5 或 4 英寸视图吗?非常感谢!
【问题讨论】:
我在我的应用程序中创建了一个宏,并使用它来检查设备:
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
按以下方式使用:
if (IS_IPHONE_5) {
//iPhone 5x specific code
} else {
//iPhone 4x specific code
}
注意:我的部署目标只有 iPhone,没有其他 iOS 设备,所以代码是安全的,除非未来版本的 iPhone 有其他维度。
【讨论】:
您可以将类别写入 UIDevice 以检查设备屏幕高度:
// UIDevice+Utils.h
@interface UIDevice (Utils)
@property (nonatomic, readonly) BOOL isIPhone5x;
@end
// UIDevice+Utils.m
@implementation UIDevice (Utils)
@dynamic isIPhone5x;
- (BOOL)isIPhone5x {
BOOL isIPhone5x = NO;
static CGFloat const kIPhone5Height = 568;
if (self.userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
CGRect screenBounds = [UIScreen mainScreen].bounds;
if (screenBounds.size.width == kIPhone5Height || screenBounds.size.height == kIPhone5Height) {
isIPhone5x = YES;
}
}
return isIPhone5x;
}
@end
// Usage
if ([UIDevice currentDevice].isIPhone5x) {
// Use 4 inch view here
} else {
// Use 3.5 inch view here
【讨论】:
另请参考链接Determine device (iPhone, iPod Touch) with iPhone SDK
参考链接:https://gist.github.com/Jaybles/1323251。您需要在项目中包含 UIDeviceHardware.h 和 UIDeviceHardware.m 文件
UIDeviceHardware *h=[[UIDeviceHardware alloc] init];
[self setDeviceModel:[h platformString]];
[h release];
希望这会有所帮助。
【讨论】: