Asaf R
Asaf R

Reputation: 33

xcode screen size is fixed - not changing per device

I'm trying to set a storyboard for the 4 inch screen to be displayed. I created 2 storyboards, name named and put the code in the

appdelegate.m

But i'm getting the same screen size every time (480) , no matter what simulator i'm using. i have tried all the code i found here on stackoverflow like :

CGSize screenSize = [[UIScreen mainScreen] bounds].size;

and taking the height from this.

I have tried other ways as well , but still getting same screen size - 480 every time.

In other projects its working just fine.

Is this a bug ? should i changed something in the project setting perhaps ?

I'm developing for iOS 7 and up on Xcode 5.

Please help if you have any idea what to do..

Upvotes: 1

Views: 3718

Answers (5)

Allen Schuenke
Allen Schuenke

Reputation: 25

For the Swifter's this is what I was able to find and I have tested it. If this don't work let me know.

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var iOSDevice: CGSize!


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        iOSDevice = UIScreen.mainScreen().bounds.size

        if iOSDevice.height == 480{
            println("iPhone4")
            var MainStoryboard: UIStoryboard
            MainStoryboard = UIStoryboard(name: "iPhone4", bundle: nil)

            var initialViewController: UIViewController!
            initialViewController = MainStoryboard.instantiateInitialViewController() as! UIViewController
            self.window!.makeKeyAndVisible()
            self.window?.rootViewController = initialViewController
        }else{
            if iOSDevice.height == 568{
                println("iPhone5")
                var MainStoryboard: UIStoryboard
                MainStoryboard = UIStoryboard(name: "iPhone5", bundle: nil)

                var initialViewController: UIViewController!
                initialViewController = MainStoryboard.instantiateInitialViewController() as! UIViewController
                self.window!.makeKeyAndVisible()
                self.window?.rootViewController = initialViewController
            }else{
                if iOSDevice.height == 667{
                    println("iPhone6")
                    var MainStoryboard: UIStoryboard
                    MainStoryboard = UIStoryboard(name: "iPhone6", bundle: nil)

                    var initialViewController: UIViewController!
                    initialViewController = MainStoryboard.instantiateInitialViewController() as! UIViewController
                    self.window!.makeKeyAndVisible()
                    self.window?.rootViewController = initialViewController
                }else{
                    if iOSDevice.height == 736{
                        println("iPhone6Plus")
                        var MainStoryboard: UIStoryboard
                        MainStoryboard = UIStoryboard(name: "iPhone6Plus", bundle: nil)

                        var initialViewController: UIViewController!
                        initialViewController = MainStoryboard.instantiateInitialViewController() as! UIViewController
                        self.window!.makeKeyAndVisible()
                        self.window?.rootViewController = initialViewController
                    }
                }
            }
        }

        return true
    }

Upvotes: 0

Asaf R
Asaf R

Reputation: 33

Thank you everybody. after hours of frustration trying every line of code i could dig up, i found out that xcode has bugs...and it was indeed a bug in Xcode.

I opened a new project , copied all the files/classes/storyboard, and it works as it should !! i guess it was somekind of bug in the simulator (closing it and reopen didn't help, neither does shutting the Mac down and restart...)

Upvotes: 2

Fahim Parkar
Fahim Parkar

Reputation: 31647

In prefix.pch use below.

#define iPhone4Or5 ([[UIScreen mainScreen] bounds].size.height == 568 ? 5 : ([[UIScreen mainScreen] bounds].size.height == 480 ? 4 : 999))

It will return as below

iPhone4Or5 = 4 (iPhone 4)

iPhone4Or5 = 5 (iPhone 4)

iPhone4Or5 = 999 (iPad)

now you can use

UIStoryboard *storyBoard;
if (iPhone4Or5==4) {
    storyBoard = [UIStoryboard storyboardWithName:@"Main_4s" bundle:nil];
} else if (iPhone4Or5==5) {
    storyBoard = [UIStoryboard storyboardWithName:@"Main_5s" bundle:nil];
} else {
    storyBoard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
}

UIViewController *initViewController = [storyBoard instantiateInitialViewController];
[self.window setRootViewController:initViewController];

I just got why this is happening.

Select the 4s storyboard and go to Editor >> Apply Retina 3.5-inch factor (if you see apply retina 4-inch, don't click that option)

For 4s you should see Editor >> Apply Retina 4-Inch

Upvotes: 1

codercat
codercat

Reputation: 23301

My suggestion is create single storyboard that is make to support both screens.

In the below image representing if you have use the button in the picture below to toggle between retina 4 in and 3.5 inch. use the autosizing tool under the size inspector on the top right to get views to position themselves correctly.

how-to-update-your-apps-for-the-4-inch-iphone-5-display

enter image description here

keep in mind autolayout supports only 6.0 and above.

enter image description here

so if you have not use autolayout try with autosizing

enter image description here

Upvotes: 1

codercat
codercat

Reputation: 23301

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
#define IS_RETINA ([[UIScreen mainScreen] scale] == 2.0f)


if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize deviceResult = [[UIScreen mainScreen] bounds].size;
    if(deviceResult.height == 480)
    {
        // iPhone Classic
    }
    if(deviceResult.height == 568)
    {
        // iPhone 5
    }
}

Upvotes: 0

Related Questions