私のアプリは2つのタブを持つスコアキーパーです。タブ1はスコアキーピングが実際に行われる場所であり、タブ2には過去のゲームの履歴が含まれています。タブ1のみが回転をサポートし、回転するとナビゲーションとタブバーが非表示になります(垂直方向のスペースが十分にありません)。タブバーがカウンターに隠されているため、[履歴]タブでの回転をまったくサポートしたくありません。タブを切り替えてからタブバーを非表示にするのは非常に不快です。
最初のタブバーに表示される可能性のあるゲームにはいくつかの種類があり、すべて「CounterBase」のサブクラスです。新しいゲームが開始されると、タブバーの最初のビューコントローラーが交換されます。すべてのローテーションコードは、サブクラスではなく、CounterBaseで処理されます。
私はローテーションをサポートするために多くのことを試みましたが、それらはすべて不安定でした。私がこれまでに得た最良の結果は、アプリが初めてロードされたときにすべてが正しく機能することですが、新しいゲームが最初に開始されたときに回転が停止する(最初のビューコントローラーを交換する)ことです。
私が見つけた1つのことは、最初のView Controllerがスワップアウトされた後、アプリデリゲートのsupportedInterfaceOrientationsForWindowが呼び出されなくなったことです。
これを処理するための最良の方法は何ですか?
アップデートiPadでタブバーを使用しているのを忘れてしまいました。タブバーは常に非表示になっているからです。そこでは回転がうまく機能するので、今は二重に混乱しています。ただし、[履歴]タブには表示されないため、関連する場合と関連しない場合があります。
アップデート2カスタムナビゲーションコントローラーを使用してshouldAutorotateを実装してみました。それでも動作しませんでした。
アップデート3問題の原因となっている最初のタブを置き換えているのではなく、ナビゲーションコントローラーにpresentViewController:animated:completion:を使用してモーダルビューを表示していることを発見しました。表示されたViewControllerでshouldAutorotateをオーバーライドしようとしましたが、何も実行されません。また、UINavigationControllerで使用しているカテゴリを以下に追加しました。
これが私の現在の実装です:
タブバーコントローラーカテゴリ
-(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