The newly created UIWindow interface sideways when the application opens onto the landscape

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

enter image description here

enter image description here

The landscape is obtained by rotating to the right once.

The part of the 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 issue only occurs in iOS 8.

+5
source share
2 answers

As I understand it, the newly created UIWindow does not rotate automatically when the orientation changes. I do not know if this is new for iOS 8 or if it is a mistake, but I was able to overcome this problem with the following code snippet:

 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]; 

The 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.

+1
source

The joint application maintains a link to a window through its key- Window Property:

let w = UIApplication.sharedApplication().keyWindow

This link, however, is slightly volatile, as the system can create temporary windows and insert them as an application key window.

try instead

 [[UIApplication sharedApplication].delegate window] 
0
source

All Articles