微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)")
如果有什么错误的地方还望各位前辈能指出来

原文地址:https://www.jb51.cc/swift/327099.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐