加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift获取屏幕的宽高

发布时间:2020-12-14 02:11:28 所属栏目:百科 来源:网络整理
导读:之前写了一篇关于获取iphone屏幕宽高的方法,算是能解决ios7下的一个小bug,是用OC写的,文章地址:http://blog.csdn.net/wingsofpiano/article/details/45726729 这次用swift语言试着写了一个相同的方法,同样,粘贴到viewcontroller就能用 /* 根据系统版本号


之前写了一篇关于获取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)")
如果有什么错误的地方还望各位前辈能指出来

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读