4

过去,当我使用 Xcode 6.4 时,我已经能够根据设备大小调整字体大小等内容。这适用于针对 iOS 7 的应用程序。现在对于 Xcode 7 和 Swift 2,它只允许在 iOS 8 和更新版本中使用。它提示我用 3 个不同的选项修复它。我无法让任何选择起作用。有没有办法使用 Swift 2 为旧的 iOS 7 设备调整 Xcode 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 中,它给了我一个 alert '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 的目标并针对不同的设备屏幕尺寸进行调整?谢谢你。

4

2 回答 2

2

我做了一些研究,发现我可以let bounds = UIScreen.mainScreen().boundsviewDidLoad(). 然后我可以设置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 时工作。

于 2015-09-22T17:57:04.663 回答
0

在运行时,使用 UIScreen 对象的boundsscale属性来了解 UIKit 如何将显示呈现给您的应用程序,以及当您需要使用显示器上的确切像素数时的nativeBoundsnativeScale 。

https://developer.apple.com/library/archive/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Displays/Displays.html

于 2018-11-29T11:34:27.853 回答