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的 s。

这样做如下:

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

我得到了答案。我发现我应该在 UINavigationController 中实现 shoulAutorotate,而不是在 UIViewController 中。我创建了另一个类(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 回答