1

I've got an Objetive-C app delegate that is part of a project I'm interfacing with my own. I'm attempting to lock orientation, but it looks like the old methods (below) no longer work. Setting the orientation in viewDidLoad rotates the screen but allows the user to rotate back to portrait. I've attempted converting to "override var" but no luck. The current solutions (link below) look to all involve an app delegate call, but I cannot locate the solution for a Swift call to an Objective C app delegate.

How to lock orientation of one view controller to portrait mode only in Swift

 override func shouldAutorotate() -> Bool {
    return false
 }

 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscapeRight
 }
Community
  • 1
  • 1
rtnlsltn
  • 31
  • 1
  • 5
  • The supported interface orientations is a variable in Swift, not a method. https://developer.apple.com/reference/uikit/uiviewcontroller/1621435-supportedinterfaceorientations – Kiran Dwarkan Apr 28 '17 at 19:20
  • As noted above, I changed "func" to "var", it doesn't seem to impact it. – rtnlsltn Apr 28 '17 at 20:53

3 Answers3

1

To force only one controller to be at landscape orientation. Manager:

class OrientationManager {

    static let shared = OrientationManager()

    /// you can set any orientation to lock
    var orientationLock = UIInterfaceOrientationMask.portrait

    /// lock orientation and force to rotate device
    func lock(for orientation: UIInterfaceOrientationMask, rotateTo rotateOrientation: UIInterfaceOrientation) {
        orientationLock = orientation
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }
}

Usage:

1) Add code to AppDelegate

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return OrientationManager.shared.orientationLock
}

2) Use in controller

open class LandscapeController: UIViewController {

    /// orientation for one controller
    override open func viewDidLoad() {
        super.viewDidLoad()
        OrientationManager.shared.lock(for: .landscape, rotateTo: .landscapeLeft)
    }

    /// set previous state of orientation or any new one
    override open func viewWillDisappear(_ animated : Bool) {
        super.viewWillDisappear(animated)
        OrientationManager.shared.lock(for: .portrait, rotateTo: .portrait)
    }
}
zdaecq
  • 91
  • 2
  • 3
0

For xcode 8.x & swift 3.x

We can control device orientation for particular screen. On AppDelegate.swift make a variable enableAllOrientation:

var enableAllOrientation = false

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if (enableAllOrientation == true){
            return UIInterfaceOrientationMask.all
        }
        return UIInterfaceOrientationMask.portrait
    } 

& mask for particular view controller as -

override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.enableAllOrientation = false
    }
Jack
  • 13,571
  • 6
  • 76
  • 98
0

Solved my problem using this link, after trying just about everything. how to lock portrait orientation for only main view using swift

I utilized the answer from JasonJasonJason and updated the code to get rid of the ->.

The below code went in my first VC

extension UINavigationController {
override open var shouldAutorotate: Bool {
    return true
}

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return (visibleViewController?.supportedInterfaceOrientations)!
}
}

Then subsequent VCs

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscape
}

Then since I had a TabBarController, I added this to the controller that pushes to the tab view. If you try to re-use the initial UINavigationController extension it won't let you, but the TabBarController ignores it. So the below extension solves it for any VC's with the TabBar

extension UITabBarController {
override open var shouldAutorotate: Bool {
    return true
}

override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscape
}
}
Community
  • 1
  • 1
rtnlsltn
  • 31
  • 1
  • 5