iOS 7.仅为一个视图控制器更改页面方向 [英] iOS 7. Change page orientation only for one view controller

查看:89
本文介绍了iOS 7.仅为一个视图控制器更改页面方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone应用程序仅支持纵向方向。我想添加到仅支持横向方向的项目视图控制器?可能吗?如果是的话我怎么能实现呢?

I have iPhone application that supports only Portrait orientation. I want to add to my project view controller that will support only Landscape orientation? Is it possible? If yes how could I achieve that?

我试图像这样包装类别文件:

I have tried to crate category file like this:

@implementation UINavigationController (Rotation_IOS7)

-(BOOL)shouldAutorotate
    {

        return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations
    {

      return UIInterfaceOrientationMaskLandscape;

    }

如果我这样做,我会收到此错误:
由于未捕获的异常而终止应用程序 UIApplicationInvalidInterfaceOrientation ,原因:支持的方向与应用程序没有共同的方向,并且shouldAutorotate返回YES

If I do this I get this error: Terminating app due to uncaught exception UIApplicationInvalidInterfaceOrientation, reason: Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES

推荐答案

我试过这个并且有效: http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

I've tried this and it works: http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

首先,将这些代码添加到AppDelegat类中。

First, add these code to your AppDelegat class.

- (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;
  }
}

然后,在横向视图控制器中,添加此方法

Then, in your landscape view controller, add this method

- (void)canRotate { }

这篇关于iOS 7.仅为一个视图控制器更改页面方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆