如何在横向模式下显示一个视图控制器?

时间:2016-05-26 06:34:26

标签: ios iphone uiinterfaceorientation

我只在一个视图控制器的横向模式下使用以下代码

- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight; // or Right of course
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}

工作正常但是当我旋转到肖像然后它正在旋转并且我使用下面的代码来呈现视图

[self presentViewController:vc animated:YES completion:nil];

我提前错过了

1 个答案:

答案 0 :(得分:0)

使用此代码

AppDelegate.h

@property (assign, nonatomic) BOOL shouldRotate;

AppDelegate.m

BOOL shouldRotate;
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
            if (self.shouldRotate)
                return UIInterfaceOrientationMaskAllButUpsideDown;
            else
                return UIInterfaceOrientationMaskPortrait;
    }

现在打开要在其中应用方向的ViewController并使用此代码

AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:YES]; // set NO to disable rotation