首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在景观上打开应用程序时,新创建的UIWindow是横向的

问在景观上打开应用程序时,新创建的UIWindow是横向的
EN

Stack Overflow用户
提问于 2015-07-21 13:21:22
回答 2查看 509关注 0票数 5

我有一个iPad应用程序,在这个应用程序的开头创建一个新的UIWindow,并在做一些资源同步时显示它。当这款应用在iPad面向人像的时候发布时,一切都很好。然而,在面向景观时,新创建的UIWindow's大小看起来很好,但是它看起来是横向的,并且它的坐标看起来都很奇怪。以下是人像和景观方向的截图:

​

​

​

​

景观一次是通过旋转一次得到的。

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

代码语言:javascript
复制
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上。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-22 07:48:13

据我所知,新创建的UIWindow不会在方向改变时自动旋转。我不知道这对于iOS 8来说是新的还是错误,但是我能够用下面的代码来克服这个问题:

代码语言:javascript
复制
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的实现如下:

代码语言:javascript
复制
#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
EN

Stack Overflow用户

发布于 2015-07-21 13:37:11

共享应用程序通过其键窗口属性维护对窗口的引用: let w = UIApplication.sharedApplication().keyWindow 但是,该引用稍微不稳定,因为系统可以创建临时窗口并将它们作为应用程序的关键窗口进行插入。

试一试

代码语言:javascript
复制
[[UIApplication sharedApplication].delegate window]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31540254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档