AppDelegate.h 加

@property (nonatomic, assign) BOOL allowRotation;

Appdelegate.m加

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
  if (self.allowRotation) {
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
  }
  return UIInterfaceOrientationMaskPortrait;
}
- (NSUInteger)supportedInterfaceOrientations
{
  return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return UIInterfaceOrientationPortrait;
}

之后在需要支持屏幕旋转的界面的特定位置上添加代码:

  • 打开屏幕旋转:
  AppDelegate  *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  delegate.allowRotation = YES;
  • 关闭屏幕旋转:
  AppDelegate  *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  delegate.allowRotation = NO;