10

So, I want to lock the orientation of my home page to portrait, and the Home page ONLY.

I am using a tab bar controller, so the initial view is the tab controller, but the view controller that appears first is the first tab, e.g. the Home page.

I would like to make it so that when the user goes to rotate the device, it WILL NOT rotate to landscape on this page. However all other pages can rotate.

I have searched around, and nothing seems to be specific to iOS 7, and the one that is specific to iOS 7 doesn't work…

Please help, thank you!

The image below describes what I DON"T want to happen, for this page.

enter image description here

Vinodh
  • 5,262
  • 4
  • 38
  • 68
n00bAppDev
  • 610
  • 3
  • 10
  • 21
  • http://stackoverflow.com/questions/12520030/how-to-force-a-uiviewcontroller-to-portait-orientation-in-ios-6 – yen Nov 07 '13 at 05:45
  • @yen, I have tried that, it doesn't work within the HomePage.m file, which it the ONLY page I want to lock orientation on. Thanks. – n00bAppDev Nov 07 '13 at 07:20

4 Answers4

7

Implement the following in your implementation

- (NSUInteger) supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskPortrait;

}

This should give you the results you are looking for!

vzm
  • 2,440
  • 6
  • 28
  • 47
  • What do you mean by implementation? The view controller for the page I want to lock? Thanks. – n00bAppDev Nov 07 '13 at 04:35
  • Yes, the `.m` file for the view controller you are working in – vzm Nov 07 '13 at 04:37
  • It only works when I put it in the .m file for the TabController, which in turn restricts every single page from rotating. When I put it in my HomePage.m file, it does not do anything. – n00bAppDev Nov 07 '13 at 04:46
6

Use this code

@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{

    return UIInterfaceOrientationMaskPortrait;

}

-(NSUInteger)supportedInterfaceOrientations
{

  return UIInterfaceOrientationMaskPortrait;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{

 return UIInterfaceOrientationPortrait;

}

@end
yen
  • 772
  • 3
  • 10
  • 1
    This code does not work when I paste it into my HomePage.m view controller file. It only works when I paste it into the TabController.m which controls all pages, meaning that EVERY single page is locked to portrait, which is not what I want. It seems to be a problem with the tab controller. – n00bAppDev Nov 07 '13 at 07:45
  • 1
    It makes no sense to return `UIInterfaceOrientationMaskPortrait` in `-(BOOL)shouldAutorotate`. – mattsson Jun 04 '14 at 16:02
  • 4
    `shouldAutorotate` should return `YES`. – Ja͢ck Jun 11 '14 at 15:25
0

The problem is, as you've rightly pointed out, that your home tab is not the topmost view controller.

From my limited knowledge on the subject I can only think of the following:

  1. Create another tab view controller and implement the methods to control orientation, i.e. shouldAutorotate and supportedInterfaceOrientations;
  2. Make this controller the first one at startup;
  3. Route the other tabs down to the original tab controller (the one that supports all orientations) using a push segue.
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

I think I found a nice solution. Well, in my case I'm using a UISplitViewController as rootController in the storyboard but the idea is the same.

  1. SubClass your rootController (In my case UISplitViewController) and Catch the shouldAutorotate() callback so you can call subviews shouldAutorotate from there.
  2. Implement shouldAutorotate() in the View you want to Lock the Rotation

    class MyUISplitViewController: UISplitViewController {
    override func shouldAutorotate() -> Bool {
        if ((self.viewControllers.last) != nil && (self.viewControllers.last!.topViewController) != nil){
            if (self.viewControllers.last!.topViewController!.respondsToSelector("shouldAutorotate"))
            {
                return self.viewControllers.last!.topViewController!.shouldAutorotate()
            }
        }
        return true
    }
    }
    

In your sub UIViewController

override func shouldAutorotate() -> Bool {
        if (UIDevice.currentDevice().userInterfaceIdiom == .Phone)
        {
            return false
        }else{
            return true
        }
    }

If you want to check the supported orientations, you can simply do the same with supportedsupportedInterfaceOrientations()

EDIT:

Don't forget to set your "MyUISplitViewController" class in your Storyboard root viewController

Mikael
  • 2,355
  • 1
  • 21
  • 45