先这样写:
let screenSize: CGRect = UIScreen.mainScreen().bounds
然后可以像这样来访问高度和宽度:
let screenWidth = screenSize.width
let screenHeight = screenSize.height
如果想要屏幕75%的宽度可以这样:
let screenWidth = screenSize.width * 0.75
做屏幕适配时可以根据屏幕高度来判断设备,例如iPhone 4s的屏幕高度是480.0,iPhone 5s的屏幕高度是568.0。
可以再viewDidLoad函数中这样写来做屏幕适配:
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenHeight = screenSize.height
print("屏幕高度:\(screenHeight)")
switch screenHeight {
case 480.0:
self.photoLeftLeading.constant = 20
self.nameTop.constant = 8
case 568.0:
self.nameTop.constant = 12
default:
break
}