Rotation only in one ViewController

后端 未结 13 1893
我在风中等你
我在风中等你 2020-12-05 03:20

I am trying to rotate one view while all other views (5) are fixed to portrait. The reason is that in that one view I want the user to watch pictures which he saved before.

13条回答
  •  半阙折子戏
    2020-12-05 04:01

    Swift 3: Add code to AppDelegate.swift

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
                if (rootViewController.responds(to: Selector(("canRotate")))) {
                    // Unlock landscape view orientations for this view controller
                    return .allButUpsideDown;
                }
            }
    
            // Only allow portrait (standard behaviour)
            return .portrait;
        }
    
        private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
            if (rootViewController == nil) { return nil }
            if (rootViewController.isKind(of: (UITabBarController).self)) {
                return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
            } else if (rootViewController.isKind(of:(UINavigationController).self)) {
                return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
            } else if (rootViewController.presentedViewController != nil) {
                return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
            }
            return rootViewController
        }
    

    Then :

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
    
        override func viewWillDisappear(_ animated : Bool) {
            super.viewWillDisappear(animated)
    
            if (self.isMovingFromParentViewController) {
                UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
            }
        }
    
        func canRotate() -> Void {}
    
    }
    

    http://www.jairobjunior.com/blog/2016/03/05/how-to-rotate-only-one-view-controller-to-landscape-in-ios-slash-swift/

提交回复
热议问题