I have a tab based application in which one tab is available in both portrait and landscape mode, all the others are only available in portrait.
I am checking agains
I think if you just use the following code and it should work well.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
// this ensures that the view that will be shown is presented in a supportred rotation
UIViewController *c = [[UIViewController alloc]init];
[viewController presentModalViewController:c animated:NO];
[viewController dismissModalViewControllerAnimated:NO];
[c release];
if ([UIViewController respondsToSelector:@selector(attemptRotationToDeviceOrientation)]) {
// this ensures that the view will be presented in the orientation of the device
// This method is only supported on iOS 5.0. iOS 4.3 users may get a little dizzy.
[UIViewController attemptRotationToDeviceOrientation];
}
}
I was able to do this, but I'm warning you now, it's a hack.
First, I created a category for UITabBarController called
UITabBarController+SelectiveRotation
:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}
This allows the TabBar to rotate freely whenever the selected tab allows an orientation. Make sure you import this file wherever you're creating your UITabBarController
(probably in your application delegate).
Then, I had my AppDelegate make itself the delegate of the tab bar, and I implemented this method:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
// this ensures that the view that will be shown is presented in a supportred rotation
UIViewController *c = [[UIViewController alloc]init];
[viewController presentModalViewController:c animated:NO];
[viewController dismissModalViewControllerAnimated:NO];
[c release];
if ([UIViewController respondsToSelector:@selector(attemptRotationToDeviceOrientation)]) {
// this ensures that the view will be presented in the orientation of the device
// This method is only supported on iOS 5.0. iOS 4.3 users may get a little dizzy.
[UIViewController attemptRotationToDeviceOrientation];
}
}
This second bit of code forces the tab bar to rotate to an acceptable orientation when the tab changes. For instance, if you go to the tab that can rotate, and go to landscape, then go to a tab that only supports portrait, it'll force the orientation change to portrait. On iOS 4.3 and earlier, going back to the rotated tab will present it in the orientation you left it in. I couldn't figure out a way around that.
I did all this because it was a client requirement, but I don't think this is actually a very usable design. I don't find too many visual tweaks to be disorienting, but forcing the device to rotate by this method is very jarring because it's instantaneous. You might want to try it out and see how you feel about it.