简体   繁体   English

无法锁定1个VC的方向

[英]Not able to lock orientation for 1 VC

I am trying to get device rotation right. 我正在尝试正确旋转设备。

  • I am testing on iPad 8.x/9.x simulator 我正在iPad 8.x / 9.x模拟器上进行测试
  • I have 4 VCs 我有4个风投
    • VC1 - Both Portrait and Landscape VC1-人像和风景
    • VC2 - Both Portrait and Landscape VC2-人像和风景
    • VC3 - Only Portrait VC3-仅纵向
    • VC4 - Both Portrait and Landscape VC4-人像和风景

Goal: to have VC3 display PortraitView at all times (same as if app orientation was fixed to portrait). 目标:让VC3始终显示PortraitView(就像将应用程序方向固定为纵向)。

I tried 我试过了

@implementation RotationAwareNavigationController

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

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

@end

In VC which is portrait 在VC中是肖像

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

But it does not work meaning view not displayed in Portrait dimensions Am I missing something? 但这不起作用,这意味着视图未显示在“纵向”尺寸中。我缺少什么吗?

I am sure it can be done as when I use ImagePickerController provided my iOS, it is fixed to Portrait. 我确信可以做到这一点,因为当我使用iOS提供的ImagePickerController时,它已固定为Portrait。 I just dont know how to do it. 我只是不知道该怎么做。

To support differing orientations in different view controllers, you will need to do a few things. 为了在不同的视图控制器中支持不同的方向,您需要做一些事情。 First, you need to check all the checkboxes for orientations you want to support in your target's General settings tab. 首先,您需要选中所有复选框,以在目标的“常规设置”标签中选择要支持的方向。

Second, anytime you call presentViewController or dismissViewController in your app, the UIApplicationDelegate method application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask will be called. 其次,每当您在应用程序中调用presentViewControllerdismissViewController ,都会调用UIApplicationDelegate方法application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask You can use this method to restrict (or allow) specific orientations each time a new view controller is presented or dismissed. 每次显示或关闭新的视图控制器时,都可以使用此方法限制(或允许)特定方向。 Unfortunately, it isn't as simple as just returning a UIInterfaceOrientationMask here. 不幸的是,它并不像只返回一个UIInterfaceOrientationMask那样简单。 You will need to find the view controller that is going to be showing on screen and return the orientations that it supports. 您将需要找到将在屏幕上显示的视图控制器,并返回其支持的方向。 Here is an example of this: 这是一个例子:

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(_:) is an example from one of my projects and is tailored to the exact view controller hierarchy in my app. findVisibleViewController(_:)是我的一个项目的示例,并且针对我的应用程序中的确切视图控制器层次进行了定制。 You will need to edit this for your own app in a way that makes sense for your hierarchy. 您需要以适合您的层次结构的方式为自己的应用程序进行编辑。

Third, you will need to implement supportedInterfaceOrientations() for most, if not all, of your view controllers. 第三,您需要为大多数(如果不是全部)视图控制器实现supportedInterfaceOrientations()

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

Finally, this only handles situations where you presented or dismissed something modally. 最后,这仅处理您以模态呈现或关闭某些内容的情况。 For show (push) segues, the orientation of the navigation controller will be used for every new view controller pushed onto the stack. 对于显示(推送)序列,导航控制器的方向将用于每个推入堆栈的新视图控制器。 If you need more fine grained control here, you will need to force the orientation to happen. 如果在这里需要更精细的控制,则需要强制进行定向。 Here is an example of that: 这是一个例子:

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

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

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