Reputation: 12749
I want all my views to be in Portrait mode. This works except when I push a UINavigationController
onto another one. In this case the views inside the secondaryNavigationController
will adhere to device orientation instead. Here is how I'm calling the UINavigationControllers
.
[secondaryNavigationController setNavigationBarHidden:YES];
[[appDelegate secondaryNavigationController navigationBar] setHidden:YES];
[mainNavigationController presentModalViewController:[appDelegate secondaryNavigationController] animated:YES];
All my views implement this method but it doesn't seem to help.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Upvotes: 0
Views: 240
Reputation: 12749
Here is the full answer that worked for me, building off of iYaniv's comment and Jacky Boy's answer.
I took iYaniv's category and made it a subclass of UINavigationController
instead of a category (which is what you should do). Then I replaced all instances of UINavigationController *
with myUINavigationController *
. Then used Jacky Boy's snippet in all of my UIViewControllers
.
/* myUINavigationController.h */
#import <UIKit/UIKit.h>
@interface myUINavigationController : UINavigationController
@end
and
/* myUINavigationController.m */
#import "myUINavigationController.h"
@interface myUINavigationController ()
@end
@implementation myUINavigationController
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end
Upvotes: 0
Reputation: 25917
Careful with:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Since it has been deprecated on iOS 6.0. At the moment here are some issues with the rotation of UIViewControllers
inside UINavigationControllers
, or UITabBarControllers
. To solve this, in your case, you should either sub-class the UINagivationController
or create a category for it (although Apple discourages the second one more than the first). You can use this (this case is for a UITabBarController
, but you can understand the logic), to check how to do the sub-classing. You can then do the following for your UIViewControllers
:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Although you are allowing to rotate (returning YES), the UIViewController
will always be in Portrait. The logic here is that if you are coming from a UIViewController
that's on Landscape, if you were returning NO, your UIViewController
, would stay in Landscape.
Upvotes: 1