Reputation: 231
I want my tab bar application with three tabs in landscape mode, but the third tab must be in portrait. I ended up converting the whole app to landscape using
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
in every view controllers as tab bar application requires all its sub view controllers to be in landscape for this.
My .plist file have Landscape option first and then added portrait.
How can I make third tab to rotate to portrait?
Upvotes: 0
Views: 802
Reputation: 5707
The UITabBarController, sadly, doesn't handle view controllers with different rotation requirements very well. The best way to handle this is to subclass UITabBarController and in shouldAutorotate, simply pass the request on to the view controller that is on screen. The code would look like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Let's grab an array of all of the child view controllers of this tab bar controller
NSArray *childVCArray = self.viewControllers;
// We know that at 5 or more tabs, we get a "More" tab for free, which contains, assumingly,
// a more navigation controller
if (self.selectedIndex <= 3) {
UINavigationController *navController = [childVCArray objectAtIndex:self.selectedIndex];
// We're in one of the first four tabs, which we know have a top view controller of UIViewController
UIViewController *viewController = navController.topViewController;
return [viewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
else {
// This will give us a More Navigation Controller
UIViewController *viewController = [childVCArray objectAtIndex:self.selectedIndex];
return [viewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
return NO;
}
This assumes that you're 5+ view controllers use the tab bar's more navigation controller and are not themselves in their own uinavigationcontroller. If they were, altering this code to fit would be trivial.
I have posted this subclass up on my github, so you can copy this method or just grab the .h/.m files from here
Upvotes: 2