Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 660
pajevic Avatar asked Feb 24 '15 15:02

pajevic


People also ask

Why are my apps sideways on my Iphone?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.

How do I force an app to rotate on IOS?

Make sure that Rotation Lock is off: Swipe down from the top-right corner of your screen to open Control Center. Then tap the Rotation Lock button to make sure it's off. Turn your iPad sideways.

How do I change the orientation of my apps?

Android Settings Start by going to Settings => Display and locate the “Device rotation” setting.


1 Answers

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
like image 157
Silmaril Avatar answered Oct 01 '22 04:10

Silmaril