5

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

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

@implementation UINavigationController (Rotation_IOS7)

-(BOOL)shouldAutorotate
    {

        return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations
    {

      return UIInterfaceOrientationMaskLandscape;

    }

如果我这样做,我会收到此错误: Terminating app due to uncaught exception UIApplicationInvalidInterfaceOrientation,原因:Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES

4

3 回答 3

4

我已经尝试过了,它有效: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 { }
于 2014-12-11T09:03:20.963 回答
1

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

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

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

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 的方向。

于 2014-05-13T09:12:03.197 回答
0

编辑:

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

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

@implementation UINavigationController (Rotation_IOS7)

-(BOOL)shouldAutorotate
    {

        return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations
    {

      return UIInterfaceOrientationMaskLandscape;

    }
于 2013-11-11T11:33:02.720 回答