ios: Custom Segue between two viewcontrollers in landscape mode

一个人想着一个人 提交于 2019-12-20 12:05:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!