Objective C iOS 9 lock viewController portrait orientation

我是研究僧i 提交于 2021-02-04 21:41:42

问题


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?


回答1:


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);
}



回答2:


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.



来源:https://stackoverflow.com/questions/34984056/objective-c-ios-9-lock-viewcontroller-portrait-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!