TheCodeMan54
TheCodeMan54

Reputation: 163

[UIScreen mainScreen].bounds gives incorrect value

I'm building a landscape app in Xcode 6. When I run it on the iOS 8.3 simulator, [UIScreen mainScreen].bounds is 667x375, but when I run it on an iPhone 6 iOS 8, [UIScreen mainScreen].bounds is 375x667. This is causing problems for my app as many elements are sized with the screen bounds. I know [UIScreen mainScreen].bounds changed in iOS 8, but that's not the issue. It should be 667x375 on both the device and simulator. I'd provide code, but I have no idea where the issue is.

Upvotes: 0

Views: 1618

Answers (3)

tpatiern
tpatiern

Reputation: 415

Here's a quick class method I use to change the screen size back to what it was before iOS 8 (switch height and width)

+ (CGSize)getScreenSize {

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

    if (screenSizeRef.width > 450 && UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
        // iPhone 6+ width on portrait is 414
        return CGSizeMake(screenSizeRef.height, screenSizeRef.width);

    }

    if (screenSizeRef.width > 900 && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        return CGSizeMake(screenSizeRef.height, screenSizeRef.width);

    }
    return screenSizeRef;
}

So instead of calling [[UIScreen mainScreen] bounds], put this method in a new class (so you can access it from anywhere in the program) and set your screen size reference to the return value of getScreenSize

Upvotes: 0

Satachito
Satachito

Reputation: 5888

iOS 8.3 iPhone6 Simulator
    Portlait
                           bounds:{{0, 0}, {375, 667}}
                 applicationFrame:{{0, 20}, {375, 647}}
                     nativeBounds:{{0, 0}, {750, 1334}}

    Landscape
                           bounds:{{0, 0}, {667, 375}}
                 applicationFrame:{{0, 0}, {667, 375}}
                     nativeBounds:{{0, 0}, {750, 1334}}

iOS 8.3 iPhone6 Device
    Portlait
                           bounds:{{0, 0}, {375, 667}}
                 applicationFrame:{{0, 20}, {375, 647}}
                     nativeBounds:{{0, 0}, {750, 1334}}
    Landscape
                           bounds:{{0, 0}, {667, 375}}
                 applicationFrame:{{0, 0}, {667, 375}}
                     nativeBounds:{{0, 0}, {750, 1334}}

On iOS 8.3, the simulator and device's results are exactly same. If reported situation only occurs on the iOS 8 device, how about targeting your application to iOS 8.3 ?

Upvotes: 1

user3349433
user3349433

Reputation: 480

On iOS8, the window's frame will change as the device orientation. I think 667x375 is the size when your device is landscape.

Upvotes: 0

Related Questions