我正在开发一个主要用于纵向模式的应用程序(除少数视图外)。
我们在iOS 8中遇到一个问题,即当显示UIViewAlert
时应用程序能够旋转,即使底层视图控制器仅支持纵向方向且其shouldAutorotate
方法返回NO。 UIAlertView
旋转到横向时旋转甚至不满,但底层视图仍处于纵向模式。如果我们在iOS 7中运行应用程序,则没有问题。
我知道在iOS 8中已弃用UIAlertView
,我们现在应该使用UIAlertController
。但是,我真的希望避免替换它,因为这意味着要编辑50多个使用UIAlertView
和UIAlertViewDelegate
的类。此外,我们仍然支持iOS 7,所以我必须有两个解决方案。当我们完全切换到iOS 8时,我宁愿只做一次。
答案 0 :(得分:9)
将其放入UIApplicationDelegate
实施
<强>夫特强>
@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)
}
}
}
<强>目标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
答案 1 :(得分:0)
在你的应用代表:(这只是一个黑客。我很高兴看到一个更优雅的解决方案)
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
UIViewController *presentedViewController = window.rootViewController.presentedViewController;
if (!presentedViewController) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
Class alertControllerClass = NSClassFromString(@"UIAlertController");
if (!alertControllerClass) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
if ([presentedViewController isKindOfClass:alertControllerClass] || [presentedViewController.presentedViewController isKindOfClass:alertControllerClass]) {
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}
答案 2 :(得分:0)
我的app遇到了类似的问题,我通过继承
解决了这个问题UIAlertViewController
并实施这些定位方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)); }
- (BOOL)shouldAutorotate {
return YES; }
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight; }