0

I need to orientation support in single view controller

I try but did not work. Right now orientation in whole app.

enter image description here

override func viewDidLoad() {
    super.viewDidLoad()

    self.lockOrientation()
}

func lockOrientation() {
    let orientationValue = UIInterfaceOrientation.portrait.rawValue
    UIDevice.current.setValue(orientationValue, forKey: "orientation")
}

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .portrait
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vivek
  • 4,916
  • 35
  • 40
  • That `UIDevice` line is a bad idea. It's a crash waiting to happen. – rmaddy Jun 03 '19 at 05:00
  • @rmaddy thank your for your comment, Can you please provide sample code which is working very well, Do you have any solution ? – Vivek Jun 03 '19 at 05:05
  • Check my answer here https://stackoverflow.com/questions/42665450/change-the-orientation-only-for-specific-view-controllers-in-ios-swift-2-3-and-s/42666574#42666574 – karthikeyan Jun 03 '19 at 05:07
  • Possible duplicate of [How to lock orientation of one view controller to portrait mode only in Swift](https://stackoverflow.com/questions/28938660/how-to-lock-orientation-of-one-view-controller-to-portrait-mode-only-in-swift) – rmaddy Jun 03 '19 at 14:56

2 Answers2

0

Put the below code in AppDelegate

Keep Device Orientation as Portrait only from Project General settings

var restrictRotation: Bool = true

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

Put below code in which you want to allow/disable orientation.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.restrictRotation(false) //TRUE TO RESTRICT ORIENTATION && FALSE TO ALLOW ORIENTATION
}

func restrictRotation(_ restriction: Bool) {
    let appDelegate = UIApplication.shared.delegate as? AppDelegate
    appDelegate?.restrictRotation = restriction
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kuldeep
  • 146
  • 2
  • 12
0

Issue solved with bmjohns answer Please visit and check this.

@bmjohns answer updated version

Swift 5

// set orientations you want to be allowed in this property by default
var orientationLock = UIInterfaceOrientationMask.all

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        self.lockOrientation()
        return true
}

func unlockOriention () {
    self.orientationLock = .all
}

func lockOrientation() {
    self.orientationLock = .portrait
}

SecondViewController.swift

While support orientation

override func viewDidLoad() {
        super.viewDidLoad()
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.unlockOriention()
    }
Community
  • 1
  • 1
Vivek
  • 4,916
  • 35
  • 40