For a graph view screen in my application the orientation has to be landscape mode, and for others screens the orientation is portrait mode.
The problem is when the graph view is on active screen and app goes in background.
When app returns back to active state the orientation of graph changes to portrait mode which is undesirable.
Is there any way to retain the orientation of graph view?
Note:The graph view controller is not connected with any navigation controller and is called programmatically after a button click event.
Thanks in advance.

- 1,099
- 2
- 13
- 30
2 Answers
There is a way to lock the orientation. Probably it will solve your problem.
I found this answer to make things easier and not writing it one more time here.
Solution: link.
Hope it helps!

- 7,899
- 4
- 56
- 63
-
thanks for replying @tungfam, the solution given in the link does not solve my problem. – Raj Feb 23 '18 at 10:56
Here are some prerequisites.
First we choose the required orientations for the application:
Second we need to add some parameters in info.plist,check that Supported Interface Orientations
contains the orientations selected in first step.
Now coming to the coding part:
In Appdelegate class, add this code to following function: func applicationDidBecomeActive(_ application: UIApplication)
//case 1: for view controllers not part UINavigationController in your.storyboard
if(self.window?.rootViewController?.presentedViewController is GraphViewController){
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
//or
//case 2: for view controllers which are part UINavigationController in your.storyboard
let currentVC = self.window?.rootViewController as? UINavigationController
if(currentVC?.viewControllers.last is GraphViewController){
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
That's it, now after running the application, if the app is sent to background (while GraphViewcontroller is on active screen) by pressing the home button and then returning to app by selection from app tray, the GraphViewcontroller
will retain the orientation.

- 1,099
- 2
- 13
- 30