iOS 8 中显示 UIAlertView 时应用程序旋转
App rotates when UIAlertView is shown in iOS 8
我正在开发一款主要用于纵向模式的应用(少数视图除外)。
我们在 iOS 8 中遇到了一个问题,当显示 UIViewAlert
时应用程序能够旋转,即使底层视图控制器仅支持纵向方向及其 shouldAutorotate
方法 returns 不。当 UIAlertView
旋转到横向时,旋转甚至还没有完全旋转,但底层视图仍处于纵向模式。如果我们 运行 中的应用程序在 iOS 7.
中就没有问题
我知道 UIAlertView
在 iOS 8 中已被弃用,我们现在应该使用 UIAlertController
。但是,我真的很想避免必须替换它,因为这意味着编辑 50+ 类 使用 UIAlertView
和 UIAlertViewDelegate
。此外,我们仍然支持 iOS 7,所以我必须同时拥有这两种解决方案。当我们完全切换到 iOS 8.
时,我宁愿只需要这样做一次
在您的应用程序委托中:(这只是一个 hack。我很高兴看到更优雅的解决方案)
- (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;
}
我的应用程序遇到了类似的问题,我通过子类化
解决了它
UIAlertViewController
并实施这些定位方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)); }
- (BOOL)shouldAutorotate {
return YES; }
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight; }
只需将其放入您的 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 8 中遇到了一个问题,当显示 UIViewAlert
时应用程序能够旋转,即使底层视图控制器仅支持纵向方向及其 shouldAutorotate
方法 returns 不。当 UIAlertView
旋转到横向时,旋转甚至还没有完全旋转,但底层视图仍处于纵向模式。如果我们 运行 中的应用程序在 iOS 7.
我知道 UIAlertView
在 iOS 8 中已被弃用,我们现在应该使用 UIAlertController
。但是,我真的很想避免必须替换它,因为这意味着编辑 50+ 类 使用 UIAlertView
和 UIAlertViewDelegate
。此外,我们仍然支持 iOS 7,所以我必须同时拥有这两种解决方案。当我们完全切换到 iOS 8.
在您的应用程序委托中:(这只是一个 hack。我很高兴看到更优雅的解决方案)
- (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;
}
我的应用程序遇到了类似的问题,我通过子类化
解决了它UIAlertViewController
并实施这些定位方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)); }
- (BOOL)shouldAutorotate {
return YES; }
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight; }
只需将其放入您的 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