iOS - 将特定的UIViewController锁定到特定方向

Sye*_*man 3 iphone objective-c uiviewcontroller uiinterfaceorientation ios

我的应用程序支持所有4方向,我有一个UIViewController这是LandscapeRight.我正在使用UINavigationController推动它UIViewController,我希望它UIViewController只是在UIInterfaceOrientationLandscapeRight,但当我旋转手机时,它切换回其他方向.

-(BOOL)shouldAutorotate{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationLandscapeRight;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;
}
Run Code Online (Sandbox Code Playgroud)

Siu*_*han 8

只需删除那些shouldAutorotate,supportedInterfaceOrientations和preferredInterfaceOrientationForPresentation.

并将此添加到您想要仅显示横向的viewcontroller.

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[UIDevice currentDevice] setValue:
     [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]
                                forKey:@"orientation"];
} 
Run Code Online (Sandbox Code Playgroud)

实际上,这是来自类似问题的解决方案. 如何在iOS 8中强制查看控制器方向?