App rotates when UIAlertView is displayed in iOS 8

I am working on an application that is mainly used in portrait mode (with the exception of a few views). We are faced with a problem in iOS 8 where the application can rotate when displayed UIViewAlert, although the lining controller only supports portrait orientation, and the method shouldAutorotatereturns NO. The rotation is not yet complete, as it is UIAlertViewrotated to landscape, but the main image remains in portrait mode. No problem if we run the application in iOS 7.

I know that it is UIAlertViewdeprecated in iOS 8 and now we have to use it UIAlertController. However, I would really like to avoid replacing, as that would mean editing 50+ classes that use UIAlertViewand UIAlertViewDelegate. In addition, we still support iOS 7, so I will have to have both solutions. I would prefer to do this only once, when we make the full transition to iOS 8.

+4
source share
3 answers

Just put this in your implementation 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
+9
source

In your application application: (This is only a hack. I would be glad to see a more elegant solution)

- (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;
}
0
source

I ran into a similar problem with my application, I solved it by subclassing

UIAlertViewController

and implementing these orientation methods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)); }

- (BOOL)shouldAutorotate {
    return YES; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight; }
0
source

All Articles