【问题标题】:iOS - Lock a specific UIViewController to a specific orientation [closed]iOS - 将特定的 UIViewController 锁定到特定的方向[关闭]
【发布时间】:2015-01-16 13:35:01
【问题描述】:

我的应用程序支持每 4 个方向,我有一个 UIViewController,它位于 LandscapeRight 中。 我正在使用UINavigationController 来推动UIViewController,我希望UIViewController 仅在UIInterfaceOrientationLandscapeRight 中,但是当我旋转手机时,它会切换回其他方向。

-(BOOL)shouldAutorotate{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationLandscapeRight;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeRight;
}

【问题讨论】:

    标签: ios objective-c iphone uiviewcontroller uiinterfaceorientation


    【解决方案1】:

    只需删除那些 shouldAutorotate、supportedInterfaceOrientations 和 preferredInterfaceOrientationForPresentation。

    并将其添加到您只想显示横向的视图控制器中。

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

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

    【讨论】:

      【解决方案2】:

      您需要创建UIViewController 的子类。并在该子类中应用与界面方向相关的更改。扩展您想要使用子类锁定方向的视图控制器。我将提供一个例子。

      我创建了只显示视图控制器横向的类。

      LandscapeViewControllerUIViewController 的子类,您必须在其中处理方向。

      LandscapeViewController.h:

      #import <UIKit/UIKit.h>
      
      @interface LandscapeViewController : UIViewController
      
      @end
      

      LandscapeViewController.m:

      #import "LandscapeViewController.h"
      
      @interface LandscapeViewController ()
      
      @end
      
      @implementation LandscapeViewController
      
      - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
          self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
          if (self) {
          }
          return self;
      }
      
      - (void)viewDidLoad {
          [super viewDidLoad];
      }
      
      - (void)didReceiveMemoryWarning
      {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      }
      
      -(BOOL)shouldAutorotate {
          return YES;
      }
      
      -(NSUInteger)supportedInterfaceOrientations {
          return UIInterfaceOrientationMaskLandscape;
      }
      
      -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
          if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
              return YES;
          }
          else {
              return NO;
          }
      }
      
      @end
      

      使用上面的子类扩展你的视图控制器。

      例如:

      #import "LandscapeViewController.h"
      
      @interface SampleViewController : LandscapeViewController
      
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-24
        相关资源
        最近更新 更多