8

In my app, I have only one UIViewController in landscape mode and it works fine if I navigate within the app. But when I present UIAlertController (for showing alert view) from that landscape viewcontroller and change device orientation, it changes that landscape viewcontroller’s orientation. I tried different solutions like :

UIAlertView crashs in iOS 8.3

iOS 8.3 supported orientations crashs

but none of these work for me. How can I lock UIAlertController’s orientation? Note: My app supports iOS9.

Community
  • 1
  • 1
Bhushan B
  • 2,470
  • 4
  • 26
  • 38

2 Answers2

2

Here is other solution that just lock orientation in the UIAlertController in this way you avoid from losing the ability rotate your screen in the viewController.

PXCustomUIAlertController.h

#import <UIKit/UIKit.h>

@interface PXCustomUIAlertController : UIAlertController
@end

PXCustomUIAlertController.m

#import "PXCustomUIAlertController.h"

@interface PXCustomUIAlertController ()
@end

@implementation  PXCustomUIAlertController


- (instancetype)init
{
    self = [super init];
    return self;
}


-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}

@end

Then you can important the class and use like this:

    PXCustomUIAlertController *alertController = [PXCustomUIAlertController
                                                      alertControllerWithTitle:nil
                                                      message:nil
                                                      preferredStyle:UIAlertControllerStyleActionSheet];

...
Tiago Mendes
  • 4,572
  • 1
  • 29
  • 35
0

If you want to lock the orientation for your entire app: If you're working with XCode, expand the left file overview. Click on App (highest element), it should have the XCode icon next to it. Then you can select the supported states under Device Orientation.

If you only want to lock the orientation for one view:

override func shouldAutorotate() -> Bool {
    return false
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}
Jonas
  • 1,473
  • 2
  • 13
  • 28