过去,当我使用Xcode 6.4时,我能够根据设备大小来调整字体大小等。这是我针对iOS 7的应用程序,现在对于Xcode 7和Swift 2,它只允许使用iOS 8和更新版本。它提示我用3种不同的选项修复它。我没办法做任何选择。是否有办法为Xcode 7中的不同设备使用Swift 2对旧的iOS 7设备进行调整?
在Xcode 6.4中,它在我的viewDidLoad()中是这样的
if UIScreen.mainScreen().nativeBounds.height == 1334.0 {
//Name Details
redLabel.font = UIFont (name: "Arial", size: 13)
yellowLabel.font = UIFont (name: "Arial", size: 13)
greenLabel.font = UIFont (name: "Arial", size: 13)
blueLabel.font = UIFont (name: "Arial", size: 13)
}在Xcode 7和Swift 2中,它给了我一个警报'nativeBounds' is only available on iOS 8.0 or newer。然后,它提示用3种不同的可能的修复方法修复它:
1)如果我选择Fix-it Add 'if available' version check,它会这样做:
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
} 2)如果我选择Fix-it Add @available attribute to enclosing instance method,它会这样做:
@available(iOS 8.0, *)
override func viewDidLoad()3)如果我选择Fix-it Add @available attribute to enclosing class,它会这样做:
@available(iOS 8.0, *)
class ViewController: UIViewController {我如何解决这个问题,让它运行iOS7的目标并根据不同的设备屏幕大小进行调整?谢谢。
发布于 2015-09-22 17:57:04
我做了一些研究,发现我可以在let bounds = UIScreen.mainScreen().bounds中使用viewDidLoad()。然后,我可以基于font设置bounds.size.height和其他项目。例如:
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,我在print(bounds.size.height)上做了一个viewDidLoad()。
我可以为这两种不同的设备指定并添加更多,比如iPhone 6 Plus和iPad Retina。当我将iOS Deployment Target设置为iOS 7.0时起作用。
发布于 2018-11-29 11:34:27
在运行时,使用边界和缩放UIScreen对象的属性来了解UIKit如何将显示呈现给应用程序,以及当您需要处理显示上的确切像素数时,nativeBounds和nativeScale。
https://stackoverflow.com/questions/32705642
复制相似问题