ios – 在横向上打开应用程序时,新创建的UIWindow是横向的

ios – 在横向上打开应用程序时,新创建的UIWindow是横向的,第1张

概述我有一个iPad应用程序,我在应用程序的开头创建一个新的UIWindow,并在我进行一些资源同步时显示它.当iPad处于纵向方向时启动应用程序时,一切都很好.但是,在横向方向上,新创建的UIWindow的大小似乎很好,但它看起来很侧面,它的坐标看起来很奇怪.以下是纵向和横向方向的屏幕截图: 通过向右旋转一次获得景观. 我创建和显示UIWindow的代码片段如下: UIWindow *window= 我有一个iPad应用程序,我在应用程序的开头创建一个新的UIWindow,并在我进行一些资源同步时显示它.当iPad处于纵向方向时启动应用程序时,一切都很好.但是,在横向方向上,新创建的UIWindow的大小似乎很好,但它看起来很侧面,它的坐标看起来很奇怪.以下是纵向和横向方向的屏幕截图:

通过向右旋转一次获得景观.

我创建和显示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的新功能还是它是一个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问题的接受答案中得到了这个想法.

总结

以上是内存溢出为你收集整理的ios – 在横向上打开应用程序时,新创建的UIWindow是横向的全部内容,希望文章能够帮你解决ios – 在横向上打开应用程序时,新创建的UIWindow是横向的所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1081970.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存