【问题标题】:Newly created UIWindow is sideways when app is opened on landscape横向打开应用时,新创建的 UIWindow 是横向的
【发布时间】:2019-09-13 20:49:02
【问题描述】:

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

风景一是向右旋转一次得到的。

我创建并显示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 上出现。

【问题讨论】:

    标签: ios ipad uiwindow


    【解决方案1】:

    据我了解,新创建的 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问题的接受答案中得到了这个想法。

    【讨论】:

      【解决方案2】:

      共享应用程序通过它的 key- 窗口属性:

      let w = UIApplication.sharedApplication().keyWindow

      然而,那个参考, 稍微不稳定,因为系统可以创建临时窗口 并将它们设置为应用程序的关键窗口。

      试试吧

      [[UIApplication sharedApplication].delegate window]
      

      【讨论】:

      • 我只是使用 keyWindow 来获取它的框架,所以我认为这不是问题。
      猜你喜欢
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多