UIAlertController:supportedInterfaceOrientations 被递归调用

UIAlertController:supportedInterfaceOrientations was invoked recursively

当两个警报一个接一个出现时,我的意思是一个警报出现,另一个警报出现,应用程序崩溃。 我已经使用 UIAlertController 来显示警报。应用仅在 iOS 9 台设备上崩溃。

此时请帮助我。

这是 iOS 9 中的一个错误,它无法检索 UIAlertControllersupportedInterfaceOrientations。在寻找 UIAlertControllersupportedInterfaceOrientations 时,它似乎陷入了无限递归循环(例如,它追溯到 UIAlertControler -> UIViewController -> UINavigationController -> UITabBarController -> UIAlertController -> ...),而对 UIAlertController:supportedInterfaceOrientations 的调用实际上并不是源代码中的 implemented/overridden。

在我的解决方案中,我刚刚添加了以下代码:

extension UIAlertController {     
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }
    public override func shouldAutorotate() -> Bool {
        return false
    }
}

然后UIAlertController直接return支持的方向值,不死循环。希望对你有帮助。

编辑:Swift 3.0.1

extension UIAlertController {
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }  
    open override var shouldAutorotate: Bool {
        return false
    }
}

我的解决方案是 UIAlertViewController 的 Objective-C 类别。只需在任何使用 UIAlertController

的 类 中包含 UIAlertController+supportedInterfaceOrientations.h

UIAlertController+supportedInterfaceOrientations.h

//
//  UIAlertController+supportedInterfaceOrientations.h

#import <UIKit/UIKit.h>
@interface UIAlertController (supportedInterfaceOrientations)
@end

UIAlertController+supportedInterfaceOrientations.m

//
//  UIAlertController+supportedInterfaceOrientations.m

#import "UIAlertController+supportedInterfaceOrientations.h"

@implementation UIAlertController (supportedInterfaceOrientations)

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
#endif

@end

写一个扩展对我来说似乎合乎逻辑,但我得到了

Overriding 'shouldAutorotate' must be as available as declaration it overrides

实施时出错。但我找到了另一个解决方案。我写了一个 class,它扩展了 UIAlertController 并覆盖了那个 class 中的 supportedInterfaceOrientationsshouldAutorotate 函数。希望这有帮助。

class MyUIAlertController : UIAlertController {

       override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
              return [UIInterfaceOrientationMask.Portrait,UIInterfaceOrientationMask.PortraitUpsideDown]
       }

       override func shouldAutorotate() -> Bool {
            return false
       }
}

我在 iOS 9 测试版中遇到过这个问题。但似乎苹果已经在 iOS 9.

的最终版本中解决了

这也可以通过始终在新创建的 UIWindow 中显示警报控制器来解决。请参阅 this SO answer 了解如何创建允许您始终以这种方式显示提醒的类别。

作为对上面 Roland Keesom 回答的更新,这对我有用。主要区别在于 supportedInterfaceOrientations 函数实际上 returns 一个 UIInterfaceOrientationMask 而不是一个 Int。

并且在此变体中支持所有方向。

extension UIAlertController {

    public override func shouldAutorotate() -> Bool {
        return true
    }

    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.All
    }
}

在我的项目中,我有各种支持各种方向的屏幕,因此一个通用扩展是不够的。

我的解决方案(Swift 5.2):

import UIKit

class UIAlertControllerWithOrientationSupport : UIAlertController {

    var fixSupportedInterfaceOrientations: UIInterfaceOrientationMask = .allButUpsideDown
    var fixShouldAutorotate: Bool = true

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return fixSupportedInterfaceOrientations
    }

    override var shouldAutorotate: Bool {
        return fixShouldAutorotate
    }

}