1
votes

I have a tabBarController which contain two tab, i would like 1 tab is support orientation but another one not,how to do that? i have tried the code:

@implementation UITabBarController (CustomTabbar)

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

     if(self.tabBarController.selectedIndex==1){
    
    
    
        NSLog(@"rotate");
        return YES;
    

    } else {

    NSLog(@"no rotate");
    return  NO;
    

    }

} @end

but after i rotating the view2 to landscape mode and back to view1, view1 become landscape as well until i rotate it back to potrait,but the thing i need is view1 always remain potrait when it appear,can help?

Regards,

sathish

2

2 Answers

0
votes

On the view controller that you dont want rotating, make sure that on the:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

You return NO;

You are returning it for the tabBar controller, not the actual viewController.

0
votes

When using a UITabBar controller, all orientation messages goes to the tab bar and stop there.

So, to accomplish what you are trying to do, you have to create a new class inherited from UITabBarController and use that instead.

@interface CustomUITabBarController : UITabBarController

After that you have 2 options to control the orientation of your controllers and both of them you will have touse the shouldAutorotateToInterfaceOrientation selector.

You can either use the tab bar current selected index to return YES or NO for the orientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    if(self.selectedIndex == 0) {
        return NO;
    } else {
        return YES;
    }

}

OR you could let the controller decide for itself (as you were trying to do before)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    if (self.selectedViewController) {
        return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    } else {
        return YES;
    }
}