iosuiviewcontrolleruinavigationcontrollerxib

Trying to subclass navigation controller in order to load external view Controller with separate .xib file in landscape mode in iOS


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. How can I do it so that I can call NextViewController, and have it displayed in landscape mode?


Solution

  • 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)