5

I have an iPad application in which I create a new UIWindow at the beginning of the app and show it while I do some resource synchronization. When the app is launched while the iPad is on portrait orientation, everything's fine. However, when on landscape orientation, the newly created UIWindow's size seems fine but it appears sideways and it's coordinates seem all weird. Here are the screenshots for both portrait and landscape orientations:

enter image description here

enter image description here

The landscape one is obtained by rotating to right once.

The piece of code where I create and show the UIWindow is as follows:

UIWindow *window=[UIApplication sharedApplication].keyWindow;
self.progressWindow = [[UIWindow alloc] initWithFrame:window.frame];
self.progressWindow.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f];
self.progressWindow.windowLevel = UIWindowLevelAlert;

/* some other code to create the subviews */

[self.progressWindow makeKeyAndVisible];

This problem only occurs on iOS 8.

Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
halileohalilei
  • 2,220
  • 2
  • 25
  • 52

2 Answers2

1

As far as I understood, newly created UIWindow's do not rotate automatically when the orientation changes. I don't know whether this is new to iOS 8 or if it is a bug, but I was able to overcome the problem with the following piece of code:

UIWindow *window=[UIApplication sharedApplication].keyWindow;
self.progressWindow = [[UIWindow alloc] initWithFrame:window.frame];
self.progressWindow.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f];
self.progressWindow.windowLevel = UIWindowLevelAlert;

/* some other code to create the subviews */

[self handleOrientationChange];
[self.progressWindow makeKeyAndVisible];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOrientationChange) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

Implementation for handleOrientationChange is as follows:

#define DegreesToRadians(degrees) (degrees * M_PI / 180)

- (void)handleOrientationChange
{
    if (self.progressWindow)
    {
        // rotate the UIWindow manually
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        [self.progressWindow setTransform:[self transformForOrientation:orientation]];

        // resize the UIWindow according to the new screen size
        if ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)])
        {
            // iOS 8
            CGRect screenRect = [UIScreen mainScreen].nativeBounds;
            CGRect progressWindowFrame = self.progressWindow.frame;
            progressWindowFrame.origin.x = 0;
            progressWindowFrame.origin.y = 0;
            progressWindowFrame.size.width = screenRect.size.width / [UIScreen mainScreen].nativeScale;
            progressWindowFrame.size.height = screenRect.size.height / [UIScreen mainScreen].nativeScale;
            self.progressWindow.frame = progressWindowFrame;
        }
        else
        {
            // iOs 7 or below
            CGRect screenRect = [UIScreen mainScreen].bounds;
            CGRect progressWindowFrame = self.progressWindow.frame;
            progressWindowFrame.origin.x = 0;
            progressWindowFrame.origin.y = 0;
            progressWindowFrame.size.width = screenRect.size.width;
            progressWindowFrame.size.height = screenRect.size.height;
            self.progressWindow.frame = progressWindowFrame;
        }
    }
}

- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation {

    switch (orientation) {

        case UIInterfaceOrientationLandscapeLeft:
            return CGAffineTransformMakeRotation(-DegreesToRadians(90));

        case UIInterfaceOrientationLandscapeRight:
            return CGAffineTransformMakeRotation(DegreesToRadians(90));

        case UIInterfaceOrientationPortraitUpsideDown:
            return CGAffineTransformMakeRotation(DegreesToRadians(180));

        case UIInterfaceOrientationPortrait:
        default:
            return CGAffineTransformMakeRotation(DegreesToRadians(0));
    }
}

I got the idea from the accepted answer of this question.

Community
  • 1
  • 1
halileohalilei
  • 2,220
  • 2
  • 25
  • 52
0

The shared application maintains a reference to the window through its key- Window property:

let w = UIApplication.sharedApplication().keyWindow

That reference, however, is slightly volatile, because the system can create temporary windows and interpose them as the application’s key window.

try instead

[[UIApplication sharedApplication].delegate window]
Alaeddine
  • 6,104
  • 3
  • 28
  • 45