问题
My app is a scorekeeper with two tabs. Tab one is where scorekeeping actually takes place, and tab two contains history of past games. Only tab one supports rotation, and when rotating it hides its navigation and tab bars (there's not enough vertical space). I do not want to support rotation at all on the history tab, because the tabbar is hidden in the counter. It is quite jarring to switch tabs and then have the tab bar disappear.
There are several types of games that may be displayed in the first tab bar, all subclasses of "CounterBase". When a new game is started, the first view controller in the tabbar is swapped out. All rotation code is handled in CounterBase, and not in its subclasses.
I have tried numerous things to support rotation, but they have all been flaky. The best result I have gotten so far is to have everything work properly when the app is loaded for the first time, but for rotation to stop the first time a new game is started (swapping the first view controller).
One thing I have found is that supportedInterfaceOrientationsForWindow in the app delegate is no longer called after the first view controller is swapped out.
What is the best way to handle this?
Update I forgot I am using a tab bar on the iPad because it is always hidden. Rotation works just fine there, so now I'm doubly confused. However, the history tab never shows, which may or may not be relevant.
Update 2 I have also tried using a custom navigation controller and implementing shouldAutorotate. Still didn't work.
Update 3 I have discovered that it is not replacing the first tab that is causing the problem, but presenting a modal view in a navigation controller with presentViewController:animated:completion:. I have tried overriding shouldAutorotate in the presented view controller, but it does nothing. Also, I've added the category I'm using on UINavigationController below.
Here is my current implementation:
Tab bar controller category
-(BOOL)shouldAutorotate{
if (self.selectedIndex == 0) // First tab is Counter
return YES;
else
return NO;
}
- (NSUInteger)supportedInterfaceOrientations{
if (self.selectedIndex==0) {
return UIInterfaceOrientationMaskAll;
}
else {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
ALog(@"Tab bar selectedIndex: %d", self.selectedIndex);
return self.selectedIndex == 0;
}
CounterBase
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (UI_USER_INTERFACE_IDIOM_PAD()){
return YES;
} else if (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown){
return YES;
}
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
if (UI_USER_INTERFACE_IDIOM_PHONE())
return UIInterfaceOrientationMaskAllButUpsideDown;
else
return UIInterfaceOrientationMaskAll;
}
- (BOOL) shouldAutorotate {
return YES;
}
HistoryBase
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
BilliardsBuddyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
return appDelegate.tabBarController.selectedIndex == 0;
}
-(BOOL)shouldAutorotate{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationPortrait;
}
AppDelegate
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
ALog(@"Tab bar selectedIndex: %d", tabBarController.selectedIndex);
if (tabBarController.selectedIndex == 0 || UI_USER_INTERFACE_IDIOM_PAD()){
return UIInterfaceOrientationMaskAll;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
@implementation UINavigationController (autoRotate)
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
@end
回答1:
Well, after three days I have finally solved this. I was using Mugunth Kumar's great UIKit categories to present an action sheet. Apparently UIActionSheet does not like having a delegate set that is not a UIViewController. (Or maybe even the topmost view controller. I'm not sure.) Switching back to the standard UIActionSheet showFromTabBar fixed this problem.
来源:https://stackoverflow.com/questions/15654339/support-rotation-in-only-one-tab-on-both-ios-5-and-6