简体   繁体   English

iOS7。仅更改一个视图控制器的页面方向

[英]iOS 7. Change page orientation only for one view controller

I have iPhone application that supports only Portrait orientation. 我有仅支持纵向方向的iPhone应用程序。 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;

    }

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 如果执行此操作Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES出现以下错误:由于未捕获的异常UIApplicationInvalidInterfaceOrientation ,终止应用程序,原因: Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES

I've tried this and it works: http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/ 我已经尝试过了,并且可以使用: http : //www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

First, add these code to your AppDelegat class. 首先,将这些代码添加到您的AppDelegat类中。

- (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 然后,在您的Landscape View控制器中,添加此方法

- (void)canRotate { }

I have search through numerous topics and finally found a working solution. 我搜索了许多主题,终于找到了一个可行的解决方案。

In my example, I have two VC's: 在我的示例中,我有两个VC:

A -> VC that is embedded inside Nav. Nav中嵌入的-> VC。 Controller and should only support Portrait view. 控制器并且仅应支持纵向视图。

B -> VC that is not embedded inside a VC and should support Landscape only. B-> VC,它未嵌入VC内,并且应仅支持横向。

I would like to go from view A to view B (by pressing a button) and back to view then A with the specific orientations still correct. 我想从视图A转到视图B(按一个按钮),然后再返回到视图A,但其特定方向仍然正确。

I. Create a Category for UINavigationController and write the following in its .m file: (the code will be automatically called) I.为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];
}

II. 二。 Create a modal segue between A and B and after that between another one between B and A. 在A和B之间以及之后的B和A之间创建一个模式选择。

III. 三, Write down in each of the View Controllers .m files the following: 在每个View Controllers .m文件中记下以下内容:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

OR 要么

- (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }

After adding this code. 添加此代码后。 You will be able to change orientation for the single view B. 您将能够更改单个视图B的方向。

Edit: 编辑:

create a category in .h and then implement those methods 在.h中创建一个类别,然后实现这些方法

use these methods in the view controller where you want to support landscape 在要支持横向的视图控制器中使用这些方法

@implementation UINavigationController (Rotation_IOS7)

-(BOOL)shouldAutorotate
    {

        return YES;

    }

    -(NSUInteger)supportedInterfaceOrientations
    {

      return UIInterfaceOrientationMaskLandscape;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM