swift获取屏幕的宽高


之前写了一篇关于获取iphone屏幕宽高的方法,算是能解决ios7下的一个小bug,是用OC写的,文章地址:http://blog.csdn.net/wingsofpiano/article/details/45726729

这次用swift语言试着写了一个相同的方法,同样,粘贴到viewcontroller就能用

    /*
    根据系统版本号得到真实的宽高
    isWidth是YES,那么代表得到宽度,是NO代表得到高度
    */
    func getTrueLength(isWidth:Bool)->CGFloat{
        
        var myRect:CGRect = UIScreen.mainScreen().bounds;
        
        //得到系统的版本号
        var myDeviceVersion:Float = (UIDevice.currentDevice().systemVersion as Nsstring).floatValue
        
        var length:CGFloat = 0.0
        
        //如果版本号小于8.0,而且是横屏的话
        if(myDeviceVersion < 8.0&&(self.interfaceOrientation == UIInterfaceOrientation.LandscapeLeft||self.interfaceOrientation == UIInterfaceOrientation.LandscapeRight)){
            
            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;
    }

使用示例:

        //得到宽度
        var width:CGFloat = getTrueLength(true)
        
        println("宽度是\(width)")
        
        //得到高度
        var height:CGFloat = getTrueLength(false)
        
        println("高度是\(height)")
如果有什么错误的地方还望各位前辈能指出来

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...