Objective C iOS 9 lock viewController portrait orientation
Asked Answered
W

2

1

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?

Whizbang answered 25/1, 2016 at 2:5 Comment(0)
S
4

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);
}
Sen answered 25/1, 2016 at 5:25 Comment(0)
S
3

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.

Smashing answered 27/7, 2017 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.