ios - 当横向打开应用程序时,新创建的 UIWindow 是横向的

标签 ios ipad uiwindow

我有一个 iPad 应用程序,我在应用程序的开头创建了一个新的 UIWindow 并在我进行一些资源同步时显示它。当应用程序在 iPad 处于纵向时启动时,一切都很好。然而,当在横向上时,新创建的 UIWindow 的 大小看起来不错,但它出现在侧面并且它的坐标看起来很奇怪。以下是纵向和横向的屏幕截图:

enter image description here

enter image description here

风景一是向右旋转一次。

我创建和显示 UIWindow 的代码如下:

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

此问题仅出现在 iOS 8 上。

最佳答案

据我所知,新创建的 UIWindow 不会在方向改变时自动旋转。我不知道这是 iOS 8 的新功能还是一个错误,但我能够通过以下代码解决这个问题:

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

handleOrientationChange 的实现如下:

#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));
    }
}

我从 this 的已接受答案中得到了这个想法问题。

关于ios - 当横向打开应用程序时,新创建的 UIWindow 是横向的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31540254/

相关文章:

ios - 使用 Quicklook 框架拦截 PDF 链接点击(QLPreviewController)

iphone - 如何以编程方式将应用程序带到前台

ios - 应用程序尝试在打印前使用 UIModalTransitionStyleCoverVertical 以外的过渡样式呈现内部弹出窗口

ios - 更改 rootViewController 时 UIWindow 导致黑屏

ios - UIWindow addSubview 在 iOS 9 中不起作用

ios - 无法自动更新 TableView

iphone - UILabel lineBreakMode剪辑不剪辑文本

javascript - 如何在不使用浏览器用户代理的情况下定位 iPad

iphone - "UIWindowLevelStatusBar + 1"奇怪的键盘行为

ios - SwiftUI 预览版因 stackoverflow 崩溃