App rotates when UIAlertView is shown in iOS 8

ios, ios8, iphone, uialertview, uiviewcontroller

Solution

Just put this in your `UIApplicationDelegate` implementation

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

Problem

I am working on an app that is mostly used in portrait mode (except for a few views). We are encountering an issue in iOS 8 where the app is able to rotate when an `UIViewAlert` is shown, even though the underlaying view controller only supports portrait orientation and its `shouldAutorotate` method returns NO. The rotation is not even full as the `UIAlertView` rotates to landscape, but the underlying view remains in portrait mode. There is no problem if we run the app in iOS 7. I know that `UIAlertView` has been deprecated in iOS 8 and that we should now use `UIAlertController`. However, I would really like to avoid having to replace it as this would mean editing 50+ classes that use `UIAlertView` and `UIAlertViewDelegate`. Also, we still support iOS 7 so I would have to have both solutions. I would prefer only to have to do this once, when we make the full switch to iOS 8.

Original source