iOS版本的判断以及屏幕的宽高问题

版本的判断

// =
#define STDEVICE_Same(v)    [[UIDevice currentDevice].systemVersion compare:v options:NSNumericSearch] == NSOrderedSame
// <
#define STDEVICE_Less(v)    [[UIDevice currentDevice].systemVersion compare:v options:NSNumericSearch] == NSOrderedAscending
// >
#define STDEVICE_Than(v)    [[UIDevice currentDevice].systemVersion compare:v options:NSNumericSearch] == NSOrderedDescending
// >=
#define STDEVICE_Than_Same(v)    [[UIDevice currentDevice].systemVersion compare:v options:NSNumericSearch] != NSOrderedAscending
// <=
#define STDEVICE_Less_Same(v)    [[UIDevice currentDevice].systemVersion compare:v options:NSNumericSearch] != NSOrderedDescending

ios 8之后屏幕的宽高发生了变化

iOS7

Currently landscape: NO
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
UIScreen.mainScreen().applicationFrame: (0.0,20.0,320.0,548.0)
Currently landscape: YES
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
 
UIScreen.mainScreen().applicationFrame: (20.0,0.0,300.0,568.0)

ios8

Currently landscape: NO
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
UIScreen.mainScreen().applicationFrame: (0.0,20.0,320.0,548.0)
Currently landscape: YES
UIScreen.mainScreen().bounds: (0.0,0.0,568.0,320.0)
UIScreen.mainScreen().applicationFrame: (0.0,0.0,568.0,320.0)

因此

在ios7中UIScreen.mainScreen().bounds是固定不变的值,在ios8中他的值是随横竖屏改变的!iOS8的statusbar还会在横屏默认隐藏

新增的api iOS8 nativebounds nativeScale

Currently landscape: YES
UIScreen.mainScreen().bounds: (0.0,0.0,568.0,320.0)
UIScreen.mainScreen().applicationFrame: (0.0,20.0,568.0,300.0)
UIScreen.mainScreen().nativeBounds: (0.0,0.0,640.0,1136.0)
UIScreen.mainScreen().nativeScale: 2.0
Currently landscape: NO
UIScreen.mainScreen().bounds: (0.0,0.0,320.0,568.0)
UIScreen.mainScreen().applicationFrame: (0.0,20.0,320.0,548.0)
UIScreen.mainScreen().nativeBounds: (0.0,0.0,640.0,1136.0)
 
UIScreen.mainScreen().nativeScale: 2.0

结论iOS 8的nativebounds固定不变

写个分类

@implementation UIScreen (Additions)

+ (float)screenWidth{
    
    

    
    
    if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
            return [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale;
        } else {
            return [UIScreen mainScreen].bounds.size.height;
        }
    } else {
        if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
            return [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale;
        } else {
            return [UIScreen mainScreen].bounds.size.width;
        }
    }
}

+ (float)screenHeight{
    if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
            if ([UIApplication sharedApplication].statusBarFrame.size.width>20) {
                return [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale-20;
            }
            return [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale;
        } else {
            if ([UIApplication sharedApplication].statusBarFrame.size.width>20) {
                return [UIScreen mainScreen].bounds.size.width-20;
            }
            return [UIScreen mainScreen].bounds.size.width;
        }
    } else {
        if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
            if ([UIApplication sharedApplication].statusBarFrame.size.height>20) {
                return [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale-20;
            }
            return [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale;
        } else {
            if ([UIApplication sharedApplication].statusBarFrame.size.height>20) {
                return [UIScreen mainScreen].bounds.size.height-20;
            }
            return [UIScreen mainScreen].bounds.size.height;
        }
    }
}

+ (BOOL)isRetina{
    if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
        return [UIScreen mainScreen].nativeScale>=2;
    } else {
        return [UIScreen mainScreen].scale>=2;
    }
}

+ (float)scale{
    if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
        return [UIScreen mainScreen].nativeScale;
    } else {
        return [UIScreen mainScreen].scale;
    }
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 原帖地址:http://blog.csdn.net/phunxm/article/details/42174937...
    油菜花花花花阅读 8,878评论 2 16
  • 1.尺寸适配1.原因 iOS7中所有导航栏都为半透明,导航栏(height=44)和状态栏(height=20)不...
    LZM轮回阅读 6,383评论 1 4
  • 今天办公室的门牌变更,忍不住拍照发圈纪念。十九年,部室的名称都变了,我还在。 有些感慨,感慨什么呢? 感慨韶华易逝...
    真冉阅读 279评论 0 1
  • 秦食冠天下,魁拔牛羊羹。 手勤分饼碎,独有秘制烹。 蚕食微发汗,河宽破围城。 且观坐者谁,独席一仙翁。 (西安回民...
    凯歌儿阅读 264评论 0 1

友情链接更多精彩内容