Reputation: 2037
I have a view (InfoVC) that is rotating even if I set shouldAutoRotate to false. This is the code that is opening the view (inside a Modal)
- (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];
}
and the code that was supposed to avoid this view to rotate (inside InfoVC.m):
- (BOOL)shouldAutorotate
{
return FALSE;
}
What is wrong?
Regards!
Upvotes: 1
Views: 320
Reputation: 19030
Instead of creating a subclass of UINavigationController
, you could use a category to perform the same task (if it's required for all instances of UINavigationController). It's a lot more lightweight than the subclassing method, and doesn't require you to swap class types for pre-existing UINavigationController
s.
To do so is as follows:
UINavigationController+NoRotate.h
@interface UINavigationController(NoRotate)
- (BOOL)shouldAutorotate;
@end
UINavigationController_NoRotate.m
#import "UINavigationController+NoRotate.h"
@implementation UINavigationController (NoRotate)
- (BOOL)shouldAutorotate
{
return NO;
}
@end
From then on, if you need a UINavigationController
to no longer rotate, simply import UINavigationController+NoRotate.h
where required. As category overrides will affect all instances of the class, if you need this behaviour for only a few cases, then you will need to subclass UINavigationController, and override -(BOOL)shouldAutorotate
.
Upvotes: 2
Reputation: 2037
I got the answer. I figured out that I was supposed to implement shoulAutorotate in the UINavigationController, not in UIViewController. I created another class (subclass of UINavigationController), implemented shouldAutorotate like in this view and I used it replacing UINavigationController.
Code:
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
New code:
- (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];
}
This worked fine for me. Thanks for everybody who tried to help!
Upvotes: 0