Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock UIAlertController's orientation?

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.

like image 885
Ameet Dhas Avatar asked May 20 '16 07:05

Ameet Dhas


1 Answers

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];

...
like image 58
Tiago Mendes Avatar answered Sep 20 '22 18:09

Tiago Mendes