0

I have an iPad application which I have created using storyboards. I have created another single viewController which I have created using a separate .xib file. This viewController I need to call from the main application, and then later dismiss to return back to the main application. I am able to do this so far. My problem is that because I am using a Navigation Controller to call this secondary view controller, I am unable to load this view controller in landscape mode. I am only able to load it in portrait mode. Based on going through this forum, and from whatever research that I have done, I have learned that I need to subclass the navigation controller, and then that is how I will be able to load this secondary view controller in landscape mode.

I have included the following methods in my secondary view controller (NextViewController), but it has no effect:

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

Here is the code in the calling viewController (MainViewController), which calls NextViewController, which in turn is appearing in portrait mode, instead of the desired landscape mode:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    _nextView = [[NextLandscapeViewController alloc] initWithNibName:@"NextLandscapeViewController" bundle:nil];
    [_nextView setDelegate:(id)self];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_nextView];
    [self presentViewController:navigationController animated:YES completion:nil];

}

As I pointed out, the solution that I need is to subclass the Navigation Controller, but I honestly have never done this before, and nor do I know how to do it. Can someone show me how to do it so that I can call NextViewController, and have it displayed in landscape mode?

Thanks in advance to all who reply.

syedfa
  • 2,801
  • 1
  • 41
  • 74

1 Answers1

1

For subclass from Navigation Controller for orientation, you can try this code (as example):

// .h - file
@interface MyNavigationController : UINavigationController

@end

// .m - file
#import "MyNavigationController.h"

@implementation MyNavigationController

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

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

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

@end

upd: (This code work on ios6)

frankWhite
  • 1,523
  • 15
  • 21
  • Thanks very much for your prompt reply. Your code is working, but I noticed one minor glitch. It works exactly as I would like when I first run it, however, on subsequent runs, the whole application appears in landscape mode now. I need only the view controller that is being called to be in landscape mode only. Is there a way for me to correct this? – syedfa Aug 25 '13 at 15:43
  • hm.. I don't know.. I did in next manner - I create Coommon viewcontroller when in method supportedInterfaceOrientations return portrait orientation. All VC subclassed from it expect some which need horizontal orientation. In my case it working. You can try do it. – frankWhite Aug 25 '13 at 17:25