【发布时间】:2015-10-03 02:26:30
【问题描述】:
当两个警报一一出现时,我的意思是一个警报出现,另一个警报出现并且应用程序崩溃。
我使用UIAlertController 来显示警报。应用程序仅在 iOS 9 设备中崩溃。
此时请帮助我。
【问题讨论】:
标签: objective-c ios9 xcode7 uialertcontroller xcode7-beta3
当两个警报一一出现时,我的意思是一个警报出现,另一个警报出现并且应用程序崩溃。
我使用UIAlertController 来显示警报。应用程序仅在 iOS 9 设备中崩溃。
此时请帮助我。
【问题讨论】:
标签: objective-c ios9 xcode7 uialertcontroller xcode7-beta3
这是 iOS 9 中的一个错误,它无法为 UIAlertController 检索到 supportedInterfaceOrientations。在寻找supportedInterfaceOrientations for UIAlertController 时,它似乎陷入了无限递归循环(例如,它回溯到UIAlertControler -> UIViewController -> UINavigationController -> UITabBarController -> @987654329 @ -> ...),而对 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
【讨论】:
作为对上面 Roland Keesom 答案的更新,这对我有用。主要区别在于supportedInterfaceOrientations 函数实际上返回的是UIInterfaceOrientationMask 而不是Int。
在这个变体中,所有方向都支持。
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 beta 版本中遇到了这个问题。但似乎苹果已经在iOS 9 的最终版本中解决了。
【讨论】:
这也可以通过始终在新创建的 UIWindow 中显示警报控制器来解决。请参阅this SO answer,了解如何创建允许您始终以这种方式显示警报的类别。
【讨论】:
在我的项目中,我有各种支持各种方向的屏幕,因此一个通用扩展是不够的。
我的解决方案(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
}
}
【讨论】: