无法锁定1个VC的方向

时间:2016-07-12 17:27:42

标签: ios objective-c

我正在尝试让设备正确旋转。

  • 我在iPad 8.x / 9.x模拟器上进行测试
  • 我有4个VC
    • VC1 - 纵向和横向
    • VC2 - 纵向和横向
    • VC3 - Only Portrait
    • VC4 - 纵向和横向

目标:让VC3始终显示PortraitView(就像 app 方向固定为肖像一样)。

我试过

@implementation RotationAwareNavigationController

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    UIViewController *top = self.topViewController;
    return top.supportedInterfaceOrientations;
}

-(BOOL)shouldAutorotate {
    UIViewController *top = self.topViewController;
    return [top shouldAutorotate];
}

@end

在VC中是肖像

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

但它不起作用意味着没有在肖像尺寸中显示的视图我错过了什么?

我确信它可以完成,因为当我使用ImagePickerController提供我的iOS时,它被修复为Portrait。我只是不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

要在不同的视图控制器中支持不同的方向,您需要做一些事情。首先,您需要在目标常规设置标签中选中要支持的方向的所有复选框。

其次,只要您在应用中调用presentViewControllerdismissViewController,就会调用UIApplicationDelegate方法application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask。每次出现或关闭新视图控制器时,您都可以使用此方法限制(或允许)特定方向。不幸的是,这并不像在这里返回UIInterfaceOrientationMask那么简单。您需要找到将在屏幕上显示的视图控制器并返回它支持的方向。这是一个例子:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    guard let window = window, let rootViewController = window.rootViewController else {
        return UIDevice.currentDevice().userInterfaceIdiom == .Pad ? .All : .AllButUpsideDown  // iOS defaults
    }

    // Let the view controller being shown decide what orientation it wants to support. This method will be called anytime a new view controller is presented on screen.
    return findVisibleViewController(rootViewController: rootViewController).supportedInterfaceOrientations()
}

/// Searches the view hierarchy recursively and finds the view controller that is currently showing.
private func findVisibleViewController(rootViewController rootViewController: UIViewController) -> UIViewController {
    if let presentedViewController = rootViewController.presentedViewController {
        // Search for a modal view first.
        return self.findVisibleViewController(rootViewController: presentedViewController)
    } else if
        let navigationController = rootViewController as? UINavigationController,
        let visibleViewController = navigationController.visibleViewController {
        // Then search navigation controller's views to find its visible controller.
        return self.findVisibleViewController(rootViewController: visibleViewController)
    } else if let splitViewController = rootViewController as? UISplitViewController {
        // Then try the split view controller. This will be the true root view controller. Use the master here since the detail just shows web views.
        return self.findVisibleViewController(rootViewController: splitViewController.viewControllers[0])
    } else {
        // Guess we found the visible view controller, because none of the other conditions were met.
        return rootViewController
    }
}

findVisibleViewController(_:)是我的一个项目的示例,并根据我的应用中的确切视图控制器层次结构进行了定制。您需要以对您的层次结构有意义的方式为您自己的应用编辑此内容。

第三,您需要为大多数(如果不是全部)视图控制器实施supportedInterfaceOrientations()

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return // portrait, landscape, or combinations of those.
}

最后,这只会处理您以模态方式呈现或解除某些内容的情况。对于show(push)segues,导航控制器的方向将用于推入堆栈的每个新视图控制器。如果您需要更精细的控制,则需要强制定位。这是一个例子:

// Some other view had the screen in landscape, so force the view to return to portrait
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")