在横向打开应用程序时,新创建的UIWindow处于横向

halileohalilei

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

在此处输入图片说明

在此处输入图片说明

向右旋转一次可获得风景1。

我创建并显示的代码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上会出现此问题。

halileohalilei

据我了解,当方向改变时,新创建的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));
    }
}

我从这个问题的公认答案中得到了这个主意

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

禁用整个应用程序的横向模式

当我的整个应用程序处于纵向模式时,以横向模式全屏播放视频

错误:新创建的应用程序在离子服务上发生读取ECONNRESET

新创建的ionic3应用程序无法在Android设备上运行

流星:无法运行新创建的应用程序Win10 x64

在Service Worker更新时重新创建应用程序

使用Lambda将新创建的主键值发送回应用程序

尝试从WWDC会话重新创建基于SwiftUI的应用程序时遇到的问题

从较旧的应用程序重新创建Http标头的问题

使用scrollview重新创建kivy应用程序以用作screenmanager的屏幕

如何创建仅横向视图的ios应用程序

Android:整个应用程序的横向模式

我的应用程序中的服务没有被重新创建

如何重新创建Android Facebook应用程序的视图刷卡UX?

使用基于“种子”的数学来重新创建应用程序实例

在非Leaflet应用程序(Sigma JS)中重新创建放大/缩小按钮?

我在哪里可以找到Firebase中新创建的应用程序的链接?

Xamarin-更新创建空白应用程序

如何在Android应用程序更新上重新创建数据库?

如何从PowerShell的AzureAD v2 cmdlet访问新创建的应用程序的ObjectId?

UIImagePicker 始终以横向模式打开照片应用程序

重新创建应用程序时无法设置BottomNavigationView的活动项

如何在每次渲染闪亮的应用程序时停止重新创建数据集

为什么这个新创建的 MVC 应用程序会抛出这个错误?

cordova.plugins.diagnostic 不适用于新创建的应用程序

如何避免 Cucumber 重新创建或刷新应用程序上下文

暗/亮模式重新创建应用程序后,SwitchCompats 检查不正确

长按应用程序图标时如何重新创建触觉反馈振动

以编程方式授予管理员同意新创建的应用程序注册