Ios: Custom Segue between two viewcontrollers in landscape mode

iospushuiviewcontroller

I have two UIViewControllers that are contained in a navigatoin view controller and both in landscape mode. I want to switch between two uiviewcontroller without have an animation like push. Therefore I perform a custom segue between these two if the user clicks in the first viewcontroller a button.

#import <Foundation/Foundation.h>
#import "AppDelegate.h"

@class AppDelegate;

@interface NonAnimatedSegue : UIStoryboardSegue {

}

@property (nonatomic,assign) AppDelegate* appDelegate;

@end

And this the implementation:

#import "NonAnimatedSegue.h"

@implementation NonAnimatedSegue

@synthesize appDelegate = _appDelegate;

-(void) perform{
    self.appDelegate = [[UIApplication sharedApplication] delegate];
    UIViewController *srcViewController = (UIViewController *) self.sourceViewController;
    UIViewController *destViewController = (UIViewController *) self.destinationViewController;
[srcViewController.view removeFromSuperview];
[self.appDelegate.window addSubview:destViewController.view];
self.appDelegate.window.rootViewController=destViewController;
}

@end

In the storyboard I switched to custom segue and actually it works fine. The only problem is that the second uiviewcontroller is not displayed in landscape mode but in protrait. If I remove the custom segue and replace it with a push segue then everything works fine, the second viewcontroller is displayed in landscape mode.

So what do I have to do such that the second viewcontroller is also in landscape view if I use my custom segue?

Best Answer

The code above doesn't work because the destinationViewController cannot receive updates from UIInterfaceOrientation on it's own. It receives those updates through it's "Container View Controller" (the Navigation Controller). To get the custom segue to work properly, we need to transition to the new view through the Navigation Controller.

-(void) perform{
    [[[self sourceViewController] navigationController] pushViewController:[self destinationViewController] animated:NO];
}
Related Topic