UIScreen mainScreen bounds changing after pressing the device's power button

I am trying to prevent a strange behavior in iOS 10 where an app that is designed only for landscape orientation comes back from background (to active) with apparently portrait orientation. The screen becomes heavily distorted (squeezed vertically). This happens only after pressing the power button of an iPhone (iPhone 6s is the only device I tested).

Normally the result of

[[UIScreen mainScreen] bounds]
=
(CGRect) $7 = (origin = (x = 0, y = 0), size = (width = 568, height = 320))

But in some cases (not always) after a sequence "power button" -> "Home button" back to app (when done quickly <1s) the same value becomes:

(CGRect) $5 = (origin = (x = 0, y = 0), size = (width = 320, height = 568))

The problem resolves immediately after rotating the device to the opposite "landscape" orientation (flipping the iPhone upside down). The

mainScreen
bounds become back
WxH => 568x320

I did not do much research. I do not really know where to start. Is this a known issue ?

Replies

I'm also experiencing the same issue. When I do those powerOn/Off steps and app is back in active state everyting is alright but with faster execution - and I'm not talking Flash-the-super-hero-fast - bounds are taken from portrait orientation. Even though UIScreen bounds should be oriantation oriented they are returned with wrong values. Also rotating the device portrait/landscape does not fix this issue.


I would love some official info on that matter cause I'm not sure if this is by design or actually and issue...

Same issue here.

I just realized this was happening on an iPhone 6s with iOS 10.1.

Haven't had the chance to test any other configurations yet.

Same problem here. Did anybody figure out a fix? Going to dig into the code now...

Update:

Ok, I fixed it by changing my code here:


//snippet by Joe Booth - http://stackoverflow.com/questions/24150359/is-uiscreen-mainscreen-bounds-size-becoming-orientation-dependent-in-ios8 and modified by
CGRect iOS7StyleScreenBounds()
{
    CGRect bounds = [UIScreen mainScreen].bounds;
    if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
    {
       bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
    }
    return bounds;
}


to this instead:


//Below based on snippet by Joe Booth - http://stackoverflow.com/questions/24150359/is-uiscreen-mainscreen-bounds-size-becoming-orientation-dependent-in-ios8
CGRect iOS7StyleScreenBounds()
{
    CGRect bounds = [UIScreen mainScreen].bounds;
    if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
    {
        //old way, didn't work properly after returning from a lock screen in iOS 10.  Fixed it, but does it work with every case...
        //bounds.size = CGSizeMake(bounds.size.height, bounds.size.width);
        bounds.size = CGSizeMake(MIN(bounds.size.width, bounds.size.height), MAX(bounds.size.width, bounds.size.height));
    }
    return bounds;
}


Tested on 4 devices of varying size and iOS version, seems ok. Not sure why iOS 10 flips the x/y coords differently than iOS 8 and 9 and only when returning from a power off, I assume it's a bug.