pajevic
pajevic

Reputation: 4657

App rotates when UIAlertView is shown in iOS 8

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.

Upvotes: 5

Views: 2574

Answers (3)

Alexey Matjuk
Alexey Matjuk

Reputation: 4301

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

Upvotes: 10

Haroon
Haroon

Reputation: 695

I was facing the similar issue with my app, I resolved it by subclassing the

UIAlertViewController

and implementing the these orientation methods

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

- (BOOL)shouldAutorotate {
    return YES; }

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight; }

Upvotes: 0

Moxy
Moxy

Reputation: 4212

In your app delegate: (This is only a hack. I'd 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;
}

Upvotes: 0

Related Questions