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

前端之家收集整理的这篇文章主要介绍了iOS – 将特定的UIViewController锁定到特定方向前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序支持每4个方向,我有一个在LandscapeRight中的UIViewController.
我正在使用UINavigationController来推送UIViewController,我希望UIViewController仅在UIInterfaceOrientationLandscapeRight中,但是当我旋转手机时,它会切换回其他方向.
  1. -(BOOL)shouldAutorotate{
  2. return NO;
  3. }
  4.  
  5. -(NSUInteger)supportedInterfaceOrientations{
  6. return UIInterfaceOrientationLandscapeRight;
  7. }
  8.  
  9. -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
  10. return UIInterfaceOrientationLandscapeRight;
  11. }

解决方法

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

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

  1. -(void)viewDidAppear:(BOOL)animated{
  2.  
  3. [[UIDevice currentDevice] setValue:
  4. [NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]
  5. forKey:@"orientation"];
  6. }

实际上,这是来自类似问题的解决方案.
How to force view controller orientation in iOS 8?

猜你在找的iOS相关文章