user7097242
user7097242

Reputation: 1074

How to lock orientation for iPhone for certain view controllers - Swift?

I have 2 View Controllers, VC1 and VC2.

VC1 currently presents VC2 modally.

VC1 only orientation should be portrait but VC2 can have all orientations.

The issue is when I am in VC2 and I rotate to landscape mode and then dismiss, VC1 is in landscape mode also! That should never happen!

NOTE: There is no Navigation Controller or UITabbarcontroller

I am adding my code bellow.

Appdelagate:

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
}

Code in VC2:

 override func viewDidLoad() {
        super.viewDidLoad()
        UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
}

func canRotate() -> Void {}

Link to where I went for help and found this code Website where I found Code

Thanks so much for the help!

Upvotes: 2

Views: 7451

Answers (1)

Annie Gupta
Annie Gupta

Reputation: 2822

You need to follow the below steps to lock rotation for specific ViewControllers :-

Step 1: While creating your project, allow all the orientations. Do not select anything in below image. enter image description here Step 2: If you want VC1 to have only Portrait Orientation the implementation then add the below two functions in your ViewController Class

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.all //return the value as per the required orientation 
    }

override var shouldAutorotate: Bool {
        return false
    }

Step 3: If you wish VC2 to have all the orientation then do not add any code for it.

So the conclusion:-

In project setting, allow all the orientations for whole project. Restriction should be at ViewControllers level not at project level.

If you wish any VC to have all orientation then don't write any code.

If you wish any VC to have specific orientation then implement two functions above.

Upvotes: 8

Related Questions