iOS 7. 仅更改一个视图控制器的页面方向

2023-11-22

我的 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


我已经尝试过这个并且它有效: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 { }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS 7. 仅更改一个视图控制器的页面方向 的相关文章

随机推荐