UIAlertController
来显示弹窗。这个应用程序只在iOS 9设备上崩溃。
请在这一点上帮助我。UIAlertController
来显示弹窗。这个应用程序只在iOS 9设备上崩溃。
请在这一点上帮助我。iOS 9存在一个bug,它无法检索UIAlertController
的supportedInterfaceOrientations
。看起来在查找UIAlertController
的supportedInterfaceOrientations
时它陷入了无限递归循环(例如跟踪到UIAlertControler
-> UIViewController
-> UINavigationController
-> UITabBarController
-> UIAlertController
-> ...),而实际上在源代码中没有实现/覆盖UIAlertController:supportedInterfaceOrientations
的调用。
在我的解决方案中,我只需添加以下代码:
extension UIAlertController {
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
public override func shouldAutorotate() -> Bool {
return false
}
}
那么UIAlertController
将直接返回支持的方向值,而不会出现无限循环。希望能帮到你。
编辑: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
extension UIAlertController {
public override func shouldAutorotate() -> Bool {
return true
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All
}
}
我觉得编写扩展似乎是合理的,但在实现时遇到了错误。
覆盖“shouldAutorotate”必须像它覆盖的声明一样可用
但我找到了另一个解决方案。我编写了一个类来扩展UIAlertController,并在该类中重写了supportedInterfaceOrientations
和shouldAutorotate
函数。希望这可以帮助您。
class MyUIAlertController : UIAlertController {
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait,UIInterfaceOrientationMask.PortraitUpsideDown]
}
override func shouldAutorotate() -> Bool {
return false
}
}
我曾经在 iOS 9
的测试版中遇到过这个问题。但显然,苹果在最终发布的 iOS 9
中解决了这个问题。
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
}
}