【问题标题】:Objective C iOS 9 lock viewController portrait orientationObjective C iOS 9 锁定 viewController 纵向
【发布时间】:2016-01-25 02:05:59
【问题描述】:

升级我的项目后,iOS 9.2 我的旧代码会过时

-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

在收到旋转通知后,我发现了这个丑陋的解决方案视图设置旋转(并用动画重新绘制我的视图控制器):

-(void)didRotate:(NSNotification *)notification
{

        [UIView animateWithDuration:0 animations:^{
            NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        }];       
 }

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

}

-(void)viewWillDisappear:(BOOL)animated
{
        [super viewWillDisappear:YES];

        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

如何仅在此 ViewController 中锁定方向?

【问题讨论】:

    标签: ios objective-c ios9.2


    【解决方案1】:

    您可以使用下面的委托方法进行定向,它可以适用于正在使用代码的类。这 3 行代码足以锁定你的方向。

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return (UIInterfaceOrientationMaskPortrait);
    }
    

    【讨论】:

      【解决方案2】:

      适用于 iOS 9 及更高版本

      • 首先确定您的基本视图控制器
      • 假设它是一个导航控制器。

      导航控制器的属性:

      @property (strong, nonatomic) UINavigationController *mainNavigationController;
      

      将以上内容替换为:

      @property (strong, nonatomic) MainNavigationContrller *mainNavigationController;
      

      您的 MainNavigationContrller.m 将如下所示:

      @interface MainNavigationContrller ()
      
      @end
      
      @implementation MainNavigationContrller
      
      - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view.
      }
      
      - (BOOL)shouldAutorotate
      {
          return YES;
      }
      - (UIInterfaceOrientationMask)supportedInterfaceOrientations
      {
          if(IPHONE)
          {
              UIViewController *topVC = ((AppDelegate*)[[UIApplication sharedApplication] delegate]).yourNavigationController.topViewController;
              if([topVC isKindOfClass:[yourViewController class]])
              {
                  return UIInterfaceOrientationMaskPortrait;
              }
          }
      
          return UIInterfaceOrientationMaskAll;
      }
      

      这样,您无需删除 info.plist 条目,我们可以仅阻止特定视图的方向。

      这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 2014-04-02
        • 1970-01-01
        • 2012-12-19
        相关资源
        最近更新 更多