UIScreen.mainScreen().nativeBounds.height无法使用Xcode 7/Swift 2,目标iOS7
在过去使用Xcode 6.4时,我能够根据设备大小调整字体大小等。这是我针对iOS 7开发的应用程序。现在对于Xcode 7和Swift 2,它只允许在iOS 8和更新版本中使用。它提示我用3个不同的选项来修复它。我没有任何选择去工作。在Xcode 7中,有没有一种方法可以使用Swift 2为旧版iOS 7设备调整不同设备的功能 在Xcode 6.4中,在我的UIScreen.mainScreen().nativeBounds.height无法使用Xcode 7/Swift 2,目标iOS7,ios7,ios8,swift2,xcode7,uiscreen,Ios7,Ios8,Swift2,Xcode7,Uiscreen,在过去使用Xcode 6.4时,我能够根据设备大小调整字体大小等。这是我针对iOS 7开发的应用程序。现在对于Xcode 7和Swift 2,它只允许在iOS 8和更新版本中使用。它提示我用3个不同的选项来修复它。我没有任何选择去工作。在Xcode 7中,有没有一种方法可以使用Swift 2为旧版iOS 7设备调整不同设备的功能 在Xcode 6.4中,在我的viewDidLoad()中看起来是这样的: 在Xcode 7和Swift 2中,它向我发出一个警报“nativeBounds”仅在iOS
viewDidLoad()
中看起来是这样的:
在Xcode 7和Swift 2中,它向我发出一个警报“nativeBounds”仅在iOS 8.0或更高版本上可用。然后,它会提示使用3种不同的可能修复方法进行修复:
1) 如果我选择修复它,请添加“如果可用”版本检查
它会执行以下操作:
if #available(iOS 8.0, *) {
if UIScreen.mainScreen().nativeBounds.height == 1136.0 {
//Name Details
redKid.font = UIFont (name: "Arial", size: 13)
yellowKid.font = UIFont (name: "Arial", size: 13)
greenKid.font = UIFont (name: "Arial", size: 13)
blueKid.font = UIFont (name: "Arial", size: 13)
}
} else {
// Fallback on earlier versions
}
@available(iOS 8.0, *)
override func viewDidLoad()
@available(iOS 8.0, *)
class ViewController: UIViewController {
2) 如果我选择修复它,则向封闭实例方法添加@available属性
,它会执行以下操作:
if #available(iOS 8.0, *) {
if UIScreen.mainScreen().nativeBounds.height == 1136.0 {
//Name Details
redKid.font = UIFont (name: "Arial", size: 13)
yellowKid.font = UIFont (name: "Arial", size: 13)
greenKid.font = UIFont (name: "Arial", size: 13)
blueKid.font = UIFont (name: "Arial", size: 13)
}
} else {
// Fallback on earlier versions
}
@available(iOS 8.0, *)
override func viewDidLoad()
@available(iOS 8.0, *)
class ViewController: UIViewController {
3) 如果我选择修复它,则将@available属性添加到封闭类中
它会执行以下操作:
if #available(iOS 8.0, *) {
if UIScreen.mainScreen().nativeBounds.height == 1136.0 {
//Name Details
redKid.font = UIFont (name: "Arial", size: 13)
yellowKid.font = UIFont (name: "Arial", size: 13)
greenKid.font = UIFont (name: "Arial", size: 13)
blueKid.font = UIFont (name: "Arial", size: 13)
}
} else {
// Fallback on earlier versions
}
@available(iOS 8.0, *)
override func viewDidLoad()
@available(iOS 8.0, *)
class ViewController: UIViewController {
我如何解决这个问题,让它运行iOS7的目标,并针对不同的设备屏幕大小进行调整?
谢谢。我做了一些研究,发现我可以在viewDidLoad()中使用let bounds=UIScreen.mainScreen().bounds
。然后,我可以根据bounds.size.height
设置font
和其他项目。例如:
if bounds.size.height == 568.0 { // 4" Screen
redLabel.font = UIFont (name: "Arial", size: 15)
} else if bounds.size.height == 667.0 { // 4.7" Screen
redLabel.font = UIFont (name: "Arial", size: 18)
}
为了找到每个设备的bounds.size.height
,我在viewDidLoad()
上进行了打印(bounds.size.height)
我可以指定这两种不同的设备,并添加更多,比如iPhone6Plus和iPadRetina。在我将iOS部署目标设置为iOS 7.0时工作。在运行时,使用UIScreen对象的边界和缩放属性来了解UIKit如何向应用程序显示屏幕,以及nativeBounds和nativeScale,当您需要处理显示器上的确切像素数时
来自Apple Docs:“在iOS 8及更高版本中,屏幕的边界属性考虑了屏幕的界面方向”。但是我的知识还没有扩展到如何变得“永远有意识”。也许是通过AppDelegate