2

I'm currently working on an mobile Application which should be locked into Portrait mode only. As I can do this in my project settings it is not a problem, but I want one Viewcontroller to be shown in Landscape mode only.

I tried to disable to Portrait mode in the project settings and added this piece of code to my landscape view controller (and also the one which calls it, but with a portrait orientation):

let value =  UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()

The problem I face right now is, that this solution is not optimal. The user is still able to rotate his the device. How can I fix this?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Icetime
  • 65
  • 6
  • I have the same issue. My code worked perfect in iOS 9, but then began allowing the user to rotate in iOS 10, if they tried to. – BennyTheNerd Dec 15 '16 at 01:22

3 Answers3

0

Try to lock the auto rotate

 override var shouldAutorotate : Bool {
    // Lock autorotate
    return false
}
Rob
  • 2,649
  • 3
  • 27
  • 34
0

This worked before iOS10

viewdidload:

UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")

Also have this in the viewcontroller:

override var shouldAutorotate : Bool {
    return true
}

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscapeRight
}

override var preferredInterfaceOrientationForPresentation : UIInterfaceOrientation {
    return UIInterfaceOrientation.landscapeRight
}
BennyTheNerd
  • 3,930
  • 1
  • 21
  • 16
0

Below code will work fine.

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}} .
 //Add the below method in ViewController .   

@objc func canRotate() -> Void {}