【问题标题】:How to display a view depending on iphone model如何根据 iPhone 型号显示视图
【发布时间】:2014-04-17 08:55:03
【问题描述】:

我对 iphone 4/4S 和 iphone 5/5s/5c 有两种不同的看法。我需要根据手机型号显示正确的视图。 有人可以告诉我您如何编写应用程序来检查手机型号,然后显示 3.5 或 4 英寸视图吗?非常感谢!

【问题讨论】:

  • 自动布局可以解决您的问题吗?
  • @Larme 如何开启自动布局?

标签: ios iphone view


【解决方案1】:

我在我的应用程序中创建了一个宏,并使用它来检查设备:

#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 有其他维度。

【讨论】:

  • 你试过了吗?在我的 iPhone 4S 和 iPhone 5 上都可以正常工作。
  • 对不起,我的错,刚刚试过,两种模式都可以。我删除了我的评论。
【解决方案2】:

您可以将类别写入 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

【讨论】:

    【解决方案3】:

    您可以使用https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html

    另请参考链接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];
    

    希望这会有所帮助。

    【讨论】:

    • 找不到页面
    • 编辑了答案,请查看 Brian Robbins 的解决方案。
    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 2012-04-29
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    相关资源
    最近更新 更多