iOS强制景观在一个viewController

先生,

我正在使用mapview模块,其中风景是唯一的方向允许,但其他只有肖像。 当在设备上运行ios 7和8时,视图控制器仍然呈现为纵向,除非我必须手动将设备转为横向。 你能告诉我还有什么其他的步骤吗?

以下是我的代码

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (nonatomic) BOOL isTaskPoint; @end 

AppDelegate.m

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) return YES; return NO; } 

PreviousController.m

 MapViewController * sliderVC = [[MapViewController alloc] init ]; AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate; appDelegate.isTaskPoint = TRUE; sliderVC.modalPresentationStyle = UIModalPresentationCurrentContext; [self presentViewController:sliderVC animated:NO completion:nil]; sliderVC.view.backgroundColor = [UIColor clearColor]; // MapSwift maps = MapViewController.h - (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer { AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate; appDelegate.isTaskPoint = FALSE; [self dismissViewControllerAnimated: NO completion:nil]; } 

的MapViewController

 - (BOOL) shouldAutorotate { return YES; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeRight; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationLandscapeRight|| interfaceOrientation == UIInterfaceOrientationLandscapeLeft); } 

有一个技巧真的有用。 您可以访问状态栏并设置其方向。 下次以模态方式显示视图时,这变为活动状态。 但是,在模态显示之后,您可以移除模态显示的视angular控制器。 当你用同样的方法做这件事时,用户不会注意到任何东西。 现在该设备具有所需的方向。 您现在可以安全地推动您想要在另一个方向的视图控制器。

当从视图控制器返回时,不要忘记将其旋转回来!

看到这个问题的答案。 它带有一些代码sniplets。 强制控制器在“纵向”或“横向”中更改方向

如果你想在一些视图控制器中禁用或启用特定的方向,那么这可能对你有帮助。

如果你想在特定的方向打开一些视图,那么在viewDidLoad中使用它

 NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"];