Reputation: 716
I’m making iOS application using swift. My app has many view and all view has UITabBar as root view. I need to support landscape orientation for only one tabbar item.
How can i do that?
Upvotes: 1
Views: 1558
Reputation: 7195
I have a working solution based on this answer here: https://stackoverflow.com/a/24928474/1359306
Target Settings > General > Deployment Info
.Add this code (taken from the linked answer) to your app delegate:
internal var shouldRotate = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if shouldRotate {
return .AllButUpsideDown
} else {
return .Portrait
}
}
Then in your landscape only view controller, put the following code in viewDidAppear
(NOT viewDidLoad!) to enable both portrait and landscape view:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.shouldRotate = true
And the opposite to viewWillDisappear
to disable landscape again.
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.shouldRotate = false
I am also hiding my tabs and status bar when the app is in landscape, which feels like a much better UX than tapping a tab and automatically rotating the screen.
There are a few things which in my case I don't have to worry about.
Upvotes: 2