How to force view controller orientation in iOS 8?

前端 未结 25 2553
太阳男子
太阳男子 2020-11-22 13:11

Before iOS 8, we used below code in conjunction with supportedInterfaceOrientations and shouldAutoRotate delegate methods to force app orie

25条回答
  •  佛祖请我去吃肉
    2020-11-22 13:24

    If you are using navigationViewController you should create your own superclass for this and override:

    - (BOOL)shouldAutorotate {
      id currentViewController = self.topViewController;
    
      if ([currentViewController isKindOfClass:[SecondViewController class]])
        return NO;
    
      return YES;
    }
    

    this will disable rotation in SecondViewController but if you push your SecondViewController when your device is on portrait orientation then your SecondViewController will appear in portrait mode.

    Assume that you are using storyboard. You have to create manual segue (How to) and in your "onClick" method:

    - (IBAction)onPlayButtonClicked:(UIBarButtonItem *)sender {
      NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
      [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
      [self performSegueWithIdentifier:@"PushPlayerViewController" sender:self];
    }
    

    This will force landscape orientation before your superclass disable autorotate feature.

提交回复
热议问题