1

I believe the Title suggests what I'm trying to implement; I'm describing here below what I'm trying to achieve & how I have tried to do same but with partial success.

VC-ViewController NC-NavigationController

I have set a NC as a RootVC of the application. Written a Category for NC Overriding the supportedInterfaceOrientations & shouldAutorotate Method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

UINavigationController *appNavController = [[UINavigationController alloc]init];
appNavController.view.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];

self.window.rootViewController = appNavController;
[self.window addSubview:appNavController.view];

FirstViewController *_firstViewController = [[FirstViewController alloc]init];
[appNavController pushViewController:_firstViewController animated:YES];
[_firstViewController release];

return YES;
}

Category for UINavigationController //UINavigationController.m

@implementation UINavigationController(Rotation)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
     return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
} 

I'm Adding the FirstVC Supporting All Orientations & things work out fine here.

//FirstViewControlle.m

-(BOOL)shouldAutorotate
 {
   return YES;
 }
-(NSUInteger)supportedInterfaceOrientations
 {
   return UIInterfaceOrientationMaskAll;
 }

From first VC I'm adding another VC (SecondVC) to NC using pushViewController.

Case 1: When the orientation of firstVC is Landscape & SecondVC is pushed on NC, rotation is blocked & SecondVC is loaded in landscape.

Case 2:Now the problem arises when firstVC is in Portrait Mode & SecondVC is pushed on NC, rotation is blocked (as shouldAutorotate is set to NO) but the second VC is loaded in Portrait Mode. Here I'm unable to force orientation though supportedInterfaceOrientations for secondVC is set to UIInterfaceOrientationMaskLandscape value. Need to load the secondVC in Landscape whatever be the NC orientation & lock it in Landscape as long as secondVC is being viewed.

//SecondViewControlle.m

-(BOOL)shouldAutorotate
 {
   return NO;
 }
-(NSUInteger)supportedInterfaceOrientations
 {
   return UIInterfaceOrientationMaskLandscape;
 }

I have referred the following Q&A Force a Portrait... while Implementing the above code.

All suggestions & solutions are most welcome; Thanks in advance.

Community
  • 1
  • 1
Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50
  • 1
    You should use this recent answer from the same Q&A you referred to : http://stackoverflow.com/a/14445888/764575 Also consider returning YES for "shouldAutoRotate" , and return just one orientation for supported orientations. – Nir Golan Jan 23 '13 at 20:08

0 Answers0