UINavigationController スタック内の 1 つのビューで異なる方向をサポートしたいだけです。これどうやってするの?
また、iOS5 でも動作する必要があります。
UINavigationController スタック内の 1 つのビューで異なる方向をサポートしたいだけです。これどうやってするの?
また、iOS5 でも動作する必要があります。
iOS6 が向きを処理する方法について、私は多くの問題を抱えてきました。これがあなたが探しているものであることを願っています。
UINavigationController のカテゴリを作成し、「UINavigationController+autoRotate」という名前を付けます。
これを UINavigationController+autoRotate.h に入れます:
#import <UIKit/UIKit.h>
@interface UINavigationController (autoRotate)
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
-(BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
@end
これを UINavigationController+autoRotate.m に入れます:
#import "UINavigationController+autoRotate.h"
@implementation UINavigationController (autoRotate)
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
- (BOOL)shouldAutorotate
{
return [self.visibleViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
if (![[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"ViewController")])
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
return [self.topViewController supportedInterfaceOrientations];
}
}
@end
回転させたくないビューの場合は、次を追加します。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
回転させたいビューの場合:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIDeviceOrientationPortraitUpsideDown);
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotate
{
return YES;
}
アプリのデリゲートに、次を追加します。
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
次の場合、ソリューションは iOS 6 では機能しません (iOS 5 では問題ありません)。
vc A
縦向きのみをサポートしていますvc B
すべての方向性をサポートしていますvc B
し、 (横向きなどで)vc A
回転させ、ポップして に戻します。向きはランドスケープモードのままです...vc B
vc A
vc A
これらのメソッドをオーバーライドするために UINavigationController にカテゴリを作成しないことをお勧めします。カテゴリはそれを目的としたものではありません。Apple のコードではなく、あなたのコードが読み込まれるという保証はありません (たとえ実際に動作したとしても)。UINavigationController のサブクラスを作成し、それらのメソッドをオーバーライドすることをお勧めします。