1

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.

  • tabBar 1 - Portrait only
  • tabBar 2 - Portrait only
  • tabBar 3 - Portrait only
  • tabBar 4 - Landscape & Portrait

How can i do that?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
jeanzuck
  • 726
  • 1
  • 7
  • 17
  • Answer here: http://stackoverflow.com/a/25925127/5066675 – mbottone Jul 15 '15 at 17:26
  • This could be simpler to implement. It seems like a similar problem. http://stackoverflow.com/questions/25651969/setting-device-orientation-in-swift-ios – Güngör Basa Jul 15 '15 at 17:29
  • Okay I will manage orientation per view But I have a question. When my app are current landscape mode in tabBar 4. when I move to another tabBar my app not back to portrait mode. Should I check per view and force rotation, right ? ps.sorry for my bad english – jeanzuck Jul 15 '15 at 17:37

1 Answers1

2

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
Community
  • 1
  • 1
Patrick
  • 6,495
  • 6
  • 51
  • 78
  • A votedown with no comment :/ If this answer is wrong or can be improved then comment. I have this code in prod. – Patrick Feb 09 '17 at 13:11