【问题标题】:iOS 7. Change page orientation only for one view controlleriOS 7. 仅为一个视图控制器更改页面方向
【发布时间】:2023-10-23 20:40:02
【问题描述】:

我有只支持纵向的 iPhone 应用程序。我想添加到仅支持横向方向的项目视图控制器?是否可以?如果是的话,我怎么能做到这一点?

我曾尝试像这样创建类别文件:

@implementation UINavigationController (Rotation_IOS7)

-(BOOL)shouldAutorotate
    {

        return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations
    {

      return UIInterfaceOrientationMaskLandscape;

    }

如果我这样做,我会收到以下错误: 由于未捕获的异常而终止应用程序UIApplicationInvalidInterfaceOrientation,原因:Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES

【问题讨论】:

  • 是的,您可以点击此链接*.com/questions/19827540/…
  • 感谢您的回答日元,但我不确定是否是我的情况。首先,我的应用程序只针对 iOS 7,所以我不想使用 iOS 6 hacks。如果我以这种方式更改导航控制器代码,那么导航链中的另一个视图控制器呢?
  • 将此代码放入您的控制器并使用 UIInterfaceOrientationMaskLandscape 而不是 UIInterfaceOrientationMaskPortrait。

标签: ios uiinterfaceorientation


【解决方案1】:

我已经尝试过了,它有效:http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

首先,将这些代码添加到您的 AppDelegate 类中。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
// Get topmost/visible view controller
UIViewController *currentViewController = [self topViewController];

// Check whether it implements a dummy methods called canRotate
if ([currentViewController respondsToSelector:@selector(canRotate)]) {
    // Unlock landscape view orientations for this view controller
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

// Only allow portrait (standard behaviour)
return UIInterfaceOrientationMaskPortrait;
}

- (UIViewController*)topViewController {
  return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
  if ([rootViewController isKindOfClass:[UITabBarController class]]) {
    UITabBarController* tabBarController = (UITabBarController*)rootViewController;
    return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
  } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
    UINavigationController* navigationController = (UINavigationController*)rootViewController;
    return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
  } else if (rootViewController.presentedViewController) {
    UIViewController* presentedViewController = rootViewController.presentedViewController;
    return [self topViewControllerWithRootViewController:presentedViewController];
  } else {
    return rootViewController;
  }
}

然后,在你的横向视图控制器中,添加这个方法

- (void)canRotate { }

【讨论】:

    【解决方案2】:

    我搜索了许多主题,终于找到了一个可行的解决方案。

    在我的示例中,我有两个 VC:

    A -> 嵌入在 Nav 中的 VC。控制器,应该只支持纵向视图。

    B -> VC 没有嵌入到 VC 中,应该只支持 Landscape。

    我想从视图 A 转到视图 B(通过按一个按钮),然后返回查看 A,具体方向仍然正确。

    我。为 UINavigationController 创建一个 Category 并在其 .m 文件中写入以下内容:(代码将自动调用)

    - (NSUInteger)supportedInterfaceOrientations
    {
        NSLog(@"supportedInterfaceOrientations = %d ", [self.topViewController         supportedInterfaceOrientations]);
    
        return [self.topViewController supportedInterfaceOrientations];
    }
    
    - (BOOL)shouldAutorotate
    {
        return self.topViewController.shouldAutorotate;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    
    {
        // You do not need this method if you are not supporting earlier iOS Versions
    
        return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
    

    二。 在 A 和 B 之间创建模态序列,然后在 B 和 A 之间创建另一个模态序列。

    三。在每个 View Controllers .m 文件中写下以下内容:

    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscape;
    }
    

    - (NSUInteger)supportedInterfaceOrientations
        {
            return UIInterfaceOrientationMaskPortrait;
        }
    

    添加此代码后。您将能够更改单个视图 B 的方向。

    【讨论】:

      【解决方案3】:

      编辑:

      在 .h 中创建一个类别,然后实现这些方法

      在要支持横向的视图控制器中使用这些方法

      @implementation UINavigationController (Rotation_IOS7)
      
      -(BOOL)shouldAutorotate
          {
      
              return YES;
      
          }
      
          -(NSUInteger)supportedInterfaceOrientations
          {
      
            return UIInterfaceOrientationMaskLandscape;
      
          }
      

      【讨论】:

      • 在我的情况下,这两种方法都没有被调用。有什么想法吗?
      • 对不起,我的愚蠢问题,我是 iOS 开发的新手。我试图创建类别文件并将您的代码放在那里。无论我在哪里导入类别 .h 文件,都会在应用程序启动时调用 shouldAutorate 方法。然后我只是把这个实现放在我的视图控制器 .m 文件中。 shouldAutorated 根本没有被调用。我做错了什么?
      最近更新 更多