我正在开发一款主要用于纵向模式的应用(少数 View 除外)。
我们在 iOS 8 中遇到了一个问题,即在显示 UIViewAlert
时应用程序能够旋转,即使底层 View Controller 仅支持纵向方向并且其 shouldAutorotate
方法返回不。当 UIAlertView
旋转到横向时,旋转甚至还没有完全旋转,但底层 View 仍处于纵向模式。如果我们在 iOS 7 中运行该应用程序,则没有问题。
我知道 UIAlertView
已在 iOS 8 中弃用,我们现在应该使用 UIAlertController
。但是,我真的很想避免替换它,因为这意味着要编辑 50 多个使用 UIAlertView
和 UIAlertViewDelegate
的类。此外,我们仍然支持 iOS 7,所以我必须同时拥有这两种解决方案。当我们完全切换到 iOS 8 时,我宁愿只需要执行一次。
最佳答案
只需将其放入您的UIApplicationDelegate
实现
swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if window == self.window {
return Int(UIInterfaceOrientationMask.All.rawValue) // Mask for all supported orientations in your app
} else {
return Int(UIInterfaceOrientationMask.Portrait.rawValue) // Supported orientations for any other window (like one created for UIAlert in iOS 8)
}
}
}
objective-C
@implementation AppDelegate
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (window == self.window) {
return UIInterfaceOrientationMaskAll; // Mask for all supported orientations in your app
} else {
return UIInterfaceOrientationMaskPortrait; // Supported orientations for any other window (like one created for UIAlert in iOS 8)
}
}
@end
关于ios - 在 iOS 8 中显示 UIAlertView 时应用程序旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699275/