即使我将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;
}
有什么问题?
问候!
答案 0 :(得分: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
。
答案 1 :(得分: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];
}
这对我来说很好。感谢所有试图帮助的人!