【问题标题】:UIScreen.mainScreen().nativeBounds.height not working using Xcode 7/Swift 2, Target iOS7UIScreen.mainScreen().nativeBounds.height 无法使用 Xcode 7/Swift 2,目标 iOS7
【发布时间】:2015-12-18 18:26:22
【问题描述】:

过去,当我使用 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 中,它会给我一个警报 '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 的目标并针对不同的设备屏幕尺寸进行调整? 谢谢。

【问题讨论】:

    标签: ios7 ios8 swift2 xcode7 uiscreen


    【解决方案1】:

    我做了一些研究,发现我可以在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() 上创建了print(bounds.size.height)。

    我可以指定两种不同的设备并添加更多设备,例如 iPhone 6 Plus 和 iPad Retina。当我将iOS Deployment Target 设置为 iOS 7.0 时工作。

    【讨论】:

    • 来自 Apple Docs:“在 iOS 8 及更高版本中,屏幕的边界属性会考虑屏幕的界面方向”。但是我的知识并没有(还)延伸到如何变得“始终意识到”。也许通过 AppDelegate
    【解决方案2】:

    在运行时,使用 UIScreen 对象的 bounds 和 scale 属性来了解 UIKit 如何将显示呈现给您的应用,以及 nativeBounds 和 nativeScale,当您需要使用显示屏上的确切像素数时。

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

    【讨论】:

      猜你喜欢
      • 2016-11-11
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多