iOS 8中自定义UIWindows不能正确旋转

栏目: IOS · 发布时间: 5年前

内容简介:http://stackoverflow.com/questions/26706050/custom-uiwindows-do-not-rotate-correctly-in-ios-8

让您的应用程序进入横向模式并执行以下代码:

UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
toastWindow.hidden = NO;
toastWindow.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5f];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [toastWindow removeFromSuperview];
});

在iOS 7中,您将获得透明的蓝色叠加层,整个屏幕顶部将在5秒钟后消失.在iOS 8中,您将获得一个透明的蓝色叠加层,覆盖了一半以上的屏幕

这显然与苹果在iOS 8中的变化有关,屏幕坐标现在是面向接口而不是面向设备,但是在真正的苹果时尚中,他们似乎在风景模式和旋转中遗留了无数的错误.

我可以通过检查设备方向是否是横向,并翻转主屏幕边框的宽度和高度来“修复”,但是当Apple在iOS 9中再次更改所有内容时,这似乎是一个可怕的黑客.

CGRect frame = [[UIScreen mainScreen] bounds];

if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
    frame.size.width = frame.size.height;
    frame.size.height = [[UIScreen mainScreen] bounds].size.width;
}

UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:frame];
toastWindow.hidden = NO;
toastWindow.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5f];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [toastWindow removeFromSuperview];
});

有没有人遇到这个问题,遇到一个更好,更不脆弱的解决方案?

编辑:我知道我只能使用UIView并将其添加到关键窗口,但我想把东西放在状态栏的顶部.

因为另一个原因,你的’修复’并不好,因为它实际上并没有旋转窗口,以便文本和其他子视图以适当的方向出现.换句话说,如果你想通过其他子视图来增强窗口,那么它们将被错误地导向.

在iOS8中,您需要设置窗口的rootViewController,并且该rootViewController需要从“shouldAutoRotate”和“supportedInterfaceOrientations”返回适当的值.还有一些关于这一点: https://devforums.apple.com/message/1050398#1050398

如果您的窗口没有rootViewController,您可以有效地告诉框架窗口不应该自动旋转.在iOS7中,这并没有什么不同,因为框架并没有为你做这个工作.在iOS8中,该框架正在处理旋转,并且它认为它在您限制窗口的边界时正在执行您所请求的(通过使用nil rootViewController).

尝试这个:

@interface MyRootViewController : UIViewController
@end

@implementation MyRootViewController

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

@end

现在,在实例化之后,将rootViewController添加到您的窗口中:

UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
toastWindow.rootViewController = [[MyRootViewController alloc]init];

http://stackoverflow.com/questions/26706050/custom-uiwindows-do-not-rotate-correctly-in-ios-8


以上所述就是小编给大家介绍的《iOS 8中自定义UIWindows不能正确旋转》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

网络经济的十种策略

网络经济的十种策略

(美)凯文・凯利 / 肖华敬/任平 / 广州出版社 / 2000-06 / 26.00元

全书介绍网络经济的十个新游戏规则,分别是:蜜蜂比狮子重要;级数比加法重要;普及比稀有重要;免费比利润重要;网络比公司重要;造山比登山重要;空间比场所重要;流动比平衡重要;关系比产能重要;机会比效率重要!一起来看看 《网络经济的十种策略》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具