IOS獲取螢幕的真實寬高
阿新 • • 發佈:2019-02-08
小弟最近在開發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獲取螢幕的寬高