问题
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?
回答1:
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];
}
回答2:
you can have the destination view controller take the center/transform/bounds from the source controller (Which is already oriented properly):
-(void) perform{
self.appDelegate = [[UIApplication sharedApplication] delegate];
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
// match orientation/position
dst.view.center = src.view.center;
dst.view.transform = src.view.transform;
dst.view.bounds = src.view.bounds;
[dst.view removeFromSuperview];
[self.appDelegate.window addSubview:dst.view];
self.appDelegate.window.rootViewController=dst;
}
来源:https://stackoverflow.com/questions/8260947/ios-custom-segue-between-two-viewcontrollers-in-landscape-mode