Objective C iOS 9 lock viewController portrait orientation

1.7k Views Asked by At

After upgrade my project do iOS 9.2 my old code become obsolete

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

And I found this ugly solution, after receive a rotation notification view set rotation (And repaint with animation my view controller):

-(void)didRotate:(NSNotification *)notification
{

        [UIView animateWithDuration:0 animations:^{
            NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        }];       
 }

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

}

-(void)viewWillDisappear:(BOOL)animated
{
        [super viewWillDisappear:YES];

        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

How can I lock orientation only in this ViewController?

2

There are 2 best solutions below

0
On

You can use the below delegate method for the orientation and it can be applicable for the class where the code is being used. These 3 lines of code is fair enough to lock your orientation.

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return (UIInterfaceOrientationMaskPortrait);
}
0
On

For iOS 9 and above

  • First Identify your base view controller
  • Suppose it is a Navigation Controller.

Property for Navigation Controller:

@property (strong, nonatomic) UINavigationController *mainNavigationController;

Replace the above with:

@property (strong, nonatomic) MainNavigationContrller *mainNavigationController;

Your MainNavigationContrller.m will look like the below:

@interface MainNavigationContrller ()

@end

@implementation MainNavigationContrller

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if(IPHONE)
    {
        UIViewController *topVC = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).yourNavigationController.topViewController;
        if([topVC isKindOfClass:[yourViewController class]])
        {
            return UIInterfaceOrientationMaskPortrait;
        }
    }

    return UIInterfaceOrientationMaskAll;
}

With this, you need not to remove your info.plist entries and we can block the orientation for specific view only.

THIS WORKED FOR ME.