You might want to reconsider your navigation pattern, as it is not something you can easily do. There's a navigation controller for that purpose, so why not to use it?
If you still want to do it your way, I can only think of adding a screenshot of dismissed view controller while presenting a new one. I'll give an example in Objective C, convert it to Swift yourself if needed.
Add UIViewController+Replace.h file to your project:
#import <UIKit/UIKit.h>
@interface UIViewController (Replace)
- (void)replaceWithViewController:(UIViewController* __nonnull)viewController animated:(BOOL)animated completion:(void (^ __nullable)(void))completion;
@end
Also add UIViewController+Replace.m:
#import "UIViewController+Replace.h"
static UIImage* RenderImageFromView(UIView *view, CGRect frame)
{
UIGraphicsBeginImageContextWithOptions(frame.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return renderedImage;
}
@implementation UIViewController (Replace)
- (void)replaceWithViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void (^)(void))completion
{
UIViewController *parentController = [self presentingViewController];
NSAssert(parentController != nil, @"Cannot replace root view controller");
CGRect bounds = self.view.frame;
UIImageView *captureView = [[UIImageView alloc] initWithFrame:bounds];
captureView.image = RenderImageFromView(self.view, bounds);
[parentController.view addSubview:captureView];
[self dismissViewControllerAnimated:NO completion:^{
[parentController presentViewController:viewController animated:animated completion:^{
[captureView removeFromSuperview];
if (completion)
completion();
}];
}];
}
@end
Then do #import "UIViewController+Replace.h"
in B
and open view controller C
from it's code:
UIViewController *c = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerC"];
// or instantiate it whatever way you want
[self replaceWithViewController:c animated:YES completion:nil]
But again, it is some weird way of doing navigation. After all, modal view controllers are not intended for navigation.