1

shouldAutoRotateをfalseに設定しても、ビュー(InfoVC)が回転しています。これは、ビューを開いているコードです(モーダル内)

- (IBAction)presentInfoVC:(id)sender{
    InfoVC *infoVC = [[InfoVC alloc] init];
    UINavigationController *infoNVC = [[UINavigationController alloc] initWithRootViewController:infoVC];

    UIImage *img =[UIImage imageNamed:@"image.png"];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];



    infoNVC.navigationBar.tintColor = [UIColor lightGrayColor];
    [infoNVC.navigationBar.topItem setTitleView:imgView];
    [imgView release];

    [self presentModalViewController:infoNVC animated:YES];

    [infoVC release];

}

そして、このビューが回転するのを回避するはずだったコード(InfoVC.m内):

- (BOOL)shouldAutorotate
{
    return FALSE;    
}

なにが問題ですか?

よろしく!

4

2 に答える 2

2

のサブクラスを作成する代わりにUINavigationController、カテゴリを使用して同じタスクを実行できます(UINavigationControllerのすべてのインスタンスで必要な場合)。サブクラス化メソッドよりもはるかに軽量であり、既存UINavigationControllerのクラスタイプを交換する必要はありません。

これを行うには、次のようにします。

UINavigationController + NoRotate.h

@interface UINavigationController(NoRotate) 
- (BOOL)shouldAutorotate;
@end

UINavigationController_NoRotate.m

#import "UINavigationController+NoRotate.h"

@implementation UINavigationController (NoRotate)

- (BOOL)shouldAutorotate
{
    return NO;
}

@end

それ以降、回転を停止する必要がある場合は、必要に応じUINavigationControllerてインポートするだけUINavigationController+NoRotate.hです。カテゴリのオーバーライドはクラスのすべてのインスタンスに影響するため、この動作がごくわずかな場合に必要な場合は、UINavigationControllerをサブクラス化し、オーバーライドする必要があります-(BOOL)shouldAutorotate

于 2012-10-30T17:46:35.670 に答える
0

私は答えを得ました。UIViewControllerではなくUINavigationControllerにshoulAutorotateを実装することになっていることがわかりました。別のクラス(UINavigationControllerのサブクラス)を作成し、このビューのようにshouldAutorotateを実装し、UINavigationControllerの代わりに使用しました。

コード:

UINavigationControllerNotRotate.h

#import <UIKit/UIKit.h>

@interface UINavigationControllerNotRotate : UINavigationController

@end

UINavigationControllerNotRotate.m

#import "UINavigationControllerNotRotate.h"

@interface UINavigationControllerNotRotate ()

@end

@implementation UINavigationControllerNotRotate

- (BOOL)shouldAutorotate
{
    return FALSE;
}

@end

新しいコード:

- (IBAction)presentInfoVC:(id)sender{

    InfoVC *infoVC = [[InfoVC alloc] init];
    UINavigationControllerNotRotate *infoNVC = [[UINavigationControllerNotRotate alloc] initWithRootViewController:infoVC];

    UIImage *img =[UIImage imageNamed:@"logo_vejasp_topbar.png"];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];


    infoNVC.navigationBar.tintColor = [UIColor lightGrayColor];
    [infoNVC.navigationBar.topItem setTitleView:imgView];
    [imgView release];

    [self presentModalViewController:infoNVC animated:YES];

    [infoVC release];

}

これは私にとってはうまくいきました。助けてくれたみんなに感謝します!

于 2012-10-30T17:25:47.283 に答える