ランドスケープ モードの tabBarController と NavigationController、エピソード II
-
09-09-2019 - |
質問
UITabBarController があり、各タブは必要に応じてスタックに新しいコントローラーをプッシュする異なる UIViewController を処理します。これらのタブのうち 2 つでは、特定のコントローラーに到達したときに iPhone を回転し、横向きモードでビューを視覚化する機能が必要です。かなり苦労した結果、 shouldAutorotateToInterfaceOrientation をオーバーライドするには UITabBarController をサブクラス化することが必須であることがわかりました。ただし、実装で単純に YES を返すと、次の望ましくない副作用が発生します。
iPhone を回転すると、すべてのタブのすべてのコントローラーが自動的に横向きモードになります。
NO を返すように各コントローラーで shouldAutorotateToInterfaceOrientation をオーバーライドしても機能しません。iPhone を回転すると、コントローラーは横向きモードになります。
サブクラス化された UITabBarController に次のように shouldAutorotateToInterfaceOrientation を実装しました。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if([self selectedIndex] == 0 || [self selectedIndex] == 3)
return YES;
return NO;
}
そのため、実際に横向きモードがサポートされるのは、関心のある 2 つのタブだけです。特定のタブのスタック上の特定のコントローラーのランドスケープ モードをサポートする方法はありますか?
私は次のようなことを試みましたが、成功しませんでした
(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if([self selectedIndex] == 0 || [self selectedIndex] == 3)
{
if ([[self selectedViewController] isKindOfClass: [landscapeModeViewController class]])
return YES;
}
return NO;
}
また、デリゲートメソッドdidSelectViewControllerを使用してみましたが、成功しませんでした。ご協力をよろしくお願いいたします。ありがとう。
解決
これは私のために働いてます:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(self.selectedIndex == 0 && [[[self.viewControllers objectAtIndex:0] visibleViewController] isKindOfClass:[MyViewController class]])
return YES;
else
return NO;
}
他のヒント
ここでは、デリゲートは、現在選択されている子コントローラにshouldAutorotateToInterfaceOrientation
するために呼び出すことUITabBarControllerの拡張機能です。この拡張機能を使用すると、あなたはもうUITabBarControllerをサブクラス化する必要はありません、もう1つは期待するようにあなたは、あなたのコントローラでshouldAutorotateToInterfaceOrientation
を使用することができます。
UITabBarController + Autorotate.hます:
#import <UIKit/UIKit.h>
@interface UITabBarController (Autorotate)
@end
UITabBarController + Autorotate.mます:
#import "UITabBarController+Autorotate.h"
@implementation UITabBarController (Autorotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]])
controller = [(UINavigationController *)controller visibleViewController];
return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
@end
これをしばらくの間(アプリのタブバーコントローラーから)問題なく使用できました。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
そうすれば、適切な VC で次のことを実行できるようになります。 本物 この場合はフォト ギャラリー ビューを確認します (他には何があるでしょうか?):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
私のギャラリー ビューは、特定の Nav コントローラーのスタックの最上位にさえありません。今でも呼ばれます。
ああ、今発見したんだけど、これは しません VC が内部に潜んでいる場合に非常にうまく機能します。 詳細ビューコントローラー (4 つのメイン タブとは対照的に)。その場合、私のギャラリーVCは決して呼ばれません。これは、私がずっと呼び出してきた VC が、実際には選択したタブのナビゲーション コントローラーであるためだと思います。 それから 適切な VC、この場合は私のフォト ギャラリー VC に物事を伝播します。しかし、More VC の場合、物事はそれほどうまくいきません...ああ、そこから物事は回転的に下り坂になります。:\
Andreas による修正 (このスレッドの他の場所を参照) を使用してみましたが、無駄でした。手がかりは大歓迎です!
私は同じ問題に遭遇しました。私は回転可能とされなかったされたUIViewControllers制御するために必要。私の主な問題は、より多くのタブとありました。私はMOREタブに含まUIViewControllersのいずれかが回転したくありませんでした。
私のソリューションは、私はと呼ばれる自分のUITabBarControllerを作成することでしたMyTabBarControllerます:
@interface MyTabBarController : UITabBarController <UITabBarDelegate> {
}
それから私は、shouldAutorotateToInterfaceOrientationメソッドを実装します:
@implementation MyTabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIViewController *controller = [self selectedViewController];
if ((controller == [self moreNavigationController]) || ([self selectedIndex] == 4))
{
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
@end
私はMOREタブが選択された場合、発見する必要がありました。これは、二段階プロセスです。 MOREタブが最初に選択されている場合、APIはので、私はmoreNavigationControllerで選択したコントローラを比較するために必要な4よりselectedIndexの高い返します。
のUIViewControllerはMORE]タブ内から選択された場合は、selectedIndexのは、最終的には4であるが、selectedControllerはもうmoreNavigationControllerではなく、のUIViewControllerを選択します。
場合の((コントローラ== [自己moreNavigationController])||([自己のselectedIndex] == 4))は、この問題の世話をする。
私は自分のアプリケーションを実行すると、さて、MOREタブの私のUIViewControllersは回転しません。私はこれは私がやったのと同じ問題に実行されている他の開発者を助けることを願っています。
エミリオ
ここや他の場所で見たことから、私は次の方法を使用するソリューションをつなぎ合わせました shouldAutorotate
昔から shouldAutorotateToInterfaceOrientation
は廃止されました。
UITabBarControllerのカテゴリ内に配置しました。 これが許容されることを願っています!
// call to method shouldAutorotate replaces call to method shouldAutorotateToInterfaceOrientation (deprecated)
-(BOOL)shouldAutorotate
{ // check whether selected view controller should autorotate
UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]])
{ // in case it is a navigation controller: get visible view of that
controller = [(UINavigationController *)controller visibleViewController];
}
return [controller shouldAutorotate];
}
ありがとう、ありがとう、ありがとう。これは、これを実行する方法を考え出すに2日間となっています。あなたはnavigationControllersとtabBarControllerを持っていたときにここにあなたの大きな助けのすべての私のテイクがあります。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]])
controller = [(UINavigationController *)controller visibleViewController];
if([controller isKindOfClass:[LOCviewcontroller class]])
return YES;
else
if([controller isKindOfClass:[personWebSiteView class]])
return YES;
else return NO;
}
neophiteコーダのコードの任意の批判が常に高く評価され...ジャック