UIAlertController:supportedInterfaceOrientations是递归调用的

Ash*_*iya 39 objective-c uialertcontroller ios9 xcode7 xcode7-beta3

当两个警报一个接一个地呈现时,我意味着一个警报存在,并且在他们上面另一个警报呈现和应用程序崩溃.我曾经用来UIAlertController显示警报.应用程序仅在iOS 9设备中崩溃.

请帮助我.

小智 75

这是iOS 9中的一个错误,它无法检索supportedInterfaceOrientationsfor UIAlertController.它似乎下降到一个无限递归循环中寻找supportedInterfaceOrientationsUIAlertController(例如,它跟踪回UIAlertControler- > UIViewController- > UINavigationController- > UITabBarController- > UIAlertController- > ...),而到了呼叫UIAlertController:supportedInterfaceOrientations实际未实现/重写源码.

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

extension UIAlertController {     
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }
    public override func shouldAutorotate() -> Bool {
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)

然后UIAlertController将直接返回支持的方向值而无需无限循环.希望能帮助到你.

编辑:Swift 3.0.1

extension UIAlertController {
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }  
    open override var shouldAutorotate: Bool {
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在Xcode 7和Swift 2.1中,似乎我们需要supportedInterfaceOrientations来返回UIInterfaceOrientationMask而不是Int.那么你也不必抛出返回值. (3认同)
  • @Annie你不应该覆盖使用类别的方法,未定义将调用哪种方法,Apple强烈建议不要这样做. (2认同)

Jim*_*and 16

我的解决方案是UIAlertViewController的Objective-C类别.只需在使用UIAlertController的任何类中包含UIAlertController + supportedInterfaceOrientations.h即可

UIAlertController + supportedInterfaceOrientations.h

//
//  UIAlertController+supportedInterfaceOrientations.h

#import <UIKit/UIKit.h>
@interface UIAlertController (supportedInterfaceOrientations)
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)


Gre*_*reg 6

作为Roland Keesom回答的更新,上面的内容对我有用.主要区别在于supportedInterfaceOrientations函数实际上返回的是UIInterfaceOrientationMask而不是Int.

在此变体中,支持所有方向.

extension UIAlertController {

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

    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.All
    }
}
Run Code Online (Sandbox Code Playgroud)