IOS获取屏幕的真实宽高

来源:互联网 发布:phpyun系统源码 编辑:程序博客网 时间:2024/05/09 20:21


 小弟最近在开发IOS应用,却发现一个问题,IOS7和IOS8在横屏模式下得到的宽高是不一样的,竖屏是一样的,但是在网上却没有找到类似的解决方法,于是自己写了一个方法,如要使用,直接粘贴到viewcontroller即可

 

/* 根据系统版本号得到真实的宽高 isWidth是YES,那么代表得到宽度,是NO代表得到高度 */-(float) getTrueWidthOrHeight:(BOOL) isWidth{    //得到屏幕的大小    CGRect myRect=[UIScreen mainScreen].bounds;    float length=0.0f;    //得到系统版本号    double version = [[UIDevice currentDevice].systemVersion doubleValue];        //如果系统版本号小于8.0f,即是7.X或以下,且还是横屏    if(version<8.0f&&(self.interfaceOrientation==UIDeviceOrientationLandscapeRight||self.interfaceOrientation==UIDeviceOrientationLandscapeLeft)){        //那么要得到的宽高要反过来        if (isWidth) {            length =myRect.size.height;        }        else{            length=myRect.size.width;        }            }    else{        if (isWidth) {            length=myRect.size.width;        }        else{            length=myRect.size.height;        }    }        return length;    }


如果需要Demo的话,请在评论中写上你的邮箱,我会给你发过去的

又写了一篇博客,是用swift语言实现此功能   链接: swift获取屏幕的宽高



0 0