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

这篇博客介绍了如何在Swift中获取iPhone屏幕的真实宽度和高度,包括在iOS7环境下解决屏幕尺寸获取的小bug,提供了具体的Swift代码示例,可以直接应用到ViewController中。
1794

被折叠的 条评论
为什么被折叠?



