jeanzuck
jeanzuck

Reputation: 716

iOS app can support landscape orientation for only one tabBar item?

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

Answers (1)

Patrick
Patrick

Reputation: 7195

I have a working solution based on this answer here: https://stackoverflow.com/a/24928474/1359306

  1. So first off, make sure the supported orientations are checked in Target Settings > General > Deployment Info.

Target Settings > General > Deployment Info

  1. 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
      }
    }
    
  2. 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
    
  3. And the opposite to viewWillDisappear to disable landscape again.

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    appDelegate.shouldRotate = false
    
  4. 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.

  • Displaying a modal view from a landscape view would cause issues

Upvotes: 2

Related Questions