3

UINavigationController スタック内の 1 つのビューで異なる方向をサポートしたいだけです。これどうやってするの?

また、iOS5 でも動作する必要があります。

4

3 に答える 3

12

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;
}
于 2012-10-08T11:35:55.070 に答える
0

次の場合、ソリューションは iOS 6 では機能しません (iOS 5 では問題ありません)。

  • vc A縦向きのみをサポートしています
  • vc Bすべての方向性をサポートしています
  • からプッシュvc Bし、 (横向きなどで)vc A回転させ、ポップして に戻します。向きはランドスケープモードのままです...vc Bvc Avc A
于 2012-10-26T15:49:55.777 に答える
0

これらのメソッドをオーバーライドするために UINavigationController にカテゴリを作成しないことをお勧めします。カテゴリはそれを目的としたものではありません。Apple のコードではなく、あなたのコードが読み込まれるという保証はありません (たとえ実際に動作したとしても)。UINavigationController のサブクラスを作成し、それらのメソッドをオーバーライドすることをお勧めします。

于 2012-10-08T11:44:59.927 に答える