ios7ios8swift2xcode7uiscreen

UIScreen.mainScreen().nativeBounds.height not working using Xcode 7/Swift 2, Target iOS7


In the past when I've used Xcode 6.4, I have been able to adjust things like font sizes, etc. based on device sizes. This was for my apps that were targeted at iOS 7. Now for Xcode 7 and Swift 2 it only allows for this with iOS 8 and newer. It prompts me to fix it with 3 different option. I can't get any of the choices to work. Is there a way to adjust things for different devices in Xcode 7 using Swift 2 for older iOS 7 devices?

In Xcode 6.4, It would look like this in my 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)
}

In Xcode 7 and Swift 2, it gives me an alert 'nativeBounds' is only available on iOS 8.0 or newer. It then prompts to fix it with 3 different possible fixes:

1) If I choose Fix-it Add 'if available' version check it does this:

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) If I choose Fix-it Add @available attribute to enclosing instance method it does this:

@available(iOS 8.0, *)
override func viewDidLoad()

3) If I choose Fix-it Add @available attribute to enclosing class it does this:

@available(iOS 8.0, *)
class ViewController: UIViewController {

How can I fix this and have it run a target of iOS7 and adjust for different device screen sizes? Thank you.


Solution

  • I did some research and found that I can use let bounds = UIScreen.mainScreen().bounds in the viewDidLoad(). I then can set font and other items based on bounds.size.height. So an example would be:

    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)
    }
    

    To find the bounds.size.height for each device, I did a print(bounds.size.height) on my viewDidLoad().

    I can specify for the two different devices and add more, like the iPhone 6 Plus and iPad Retina as well. Worked when I set the iOS Deployment Target to iOS 7.0.