在景观

时间:2015-07-21 13:21:22

标签: ios ipad uiwindow

我有一个iPad应用程序,我在应用程序的开头创建一个新的UIWindow,并在我进行一些资源同步时显示它。当iPad处于纵向方向时启动应用程序时,一切都很好。但是,在横向方向上,新创建的UIWindow's尺寸似乎很好,但它看起来很侧面,它的坐标看起来很奇怪。以下是纵向和横向方向的屏幕截图:

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上。

2 个答案:

答案 0 :(得分:1)

据我所知,当方向发生变化时,新创建的UIWindow不会自动旋转。我不知道这对iOS 8是否是新手,或者是否是一个bug,但我能够通过以下代码克服这个问题:

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问题的接受答案中得到了这个想法。

答案 1 :(得分:0)

  

共享应用程序通过其维护对窗口的引用   key-窗口属性:

     

let w = UIApplication.sharedApplication().keyWindow

     

然而,该参考文献   稍微不稳定,因为系统可以创建临时窗口   并将它们作为应用程序的关键窗口插入。

尝试改为

[[UIApplication sharedApplication].delegate window]