1
votes

currently working on an app at work that uses a tab bar controller. The app will not rotate to landscape mode at all - all views inherit from a baseVieController, and in here I have implemented:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return true;
}

Now I know a tabBar controller will not rotate unless all its subviews support the orientation the view is trying to rotate to - my question is this: If I do not implement the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation method in all subviews, will it lock these subviews to portrait mode, even if I do not specify this as a desired orientation? Therefore locking the whole tabBar controller to portrait. I know similar questions have been asked before, but I couldn't find the answer to this specific question. Thanks in advance.

3

3 Answers

4
votes

You can rotate the view, just needs to over ride like below: Just add the code in the view controller class where you want rotation on (here it is for "SampleClassName")

@interface UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
@end


@implementation UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
    {
        UINavigationController *navController = (UINavigationController *) self.selectedViewController;
        if ([[navController visibleViewController] isKindOfClass:[SampleClassName class]])
            return YES;
    }
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
1
votes

If you are developing for IOS 5 using storyboards this will help i had the same issue. Typically before storyboards we might add the TabBarController to a uiview or in the appdelegate. With storyboards the storyboard view does not always have to be connected to a viewcontroller.

To fix do this

Add a new class file objective-c class in the subclass field type UITabBarController

1 - In the storyboard select the tab bar controller view

2 - Under custom class change UITabBarController to your newly created class name, i called mine MainTabBarViewController

3 - In you newly created class change this

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

to

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
        if (interfaceOrientation == UIInterfaceOrientationPortrait)
            return YES;

        if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            return YES;

        return NO;
}

Basically what is happening is you are creating a structure in Interface Builder, but that only gets you part of the way. In this case you will still have to create the companion code. This confused me at first, because i'm used to building views up from scratch using .xib, and would typically configure the tabbarcontroller from the appdelegate.

Also you can conditionally control each of these like this

if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    return NO;

if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    return NO;

if (interfaceOrientation == UIInterfaceOrientationPortrait)
    return YES;

if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    return YES;

Just return yes for what you want, return no for what you don't want. or to accept everything return yes .

0
votes

As far as your parent view controller (in your case - baseViewController) implements this method -

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return true;
}

you have no need to implement this in child view controllers as it supporting all orientation in all child view controllers.