Dimitrio
Dimitrio

Reputation: 151

Custom transition between UIViewControllers does not work

I have object that manage custom transition:

    @interface NavigationManager : NSObject <UIViewControllerAnimatedTransitioning>

    -(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext;
    -(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext;

    @property (nonatomic, assign) NSTimeInterval presentationDuration;
    @property (nonatomic, assign) NSTimeInterval dismissalDuration;
    @property (nonatomic, assign) BOOL isPresenting;


    @end

    @interface NavigationManager()

    @property (nonatomic, strong) id<UIViewControllerContextTransitioning> transitionContext;

    @end

    @implementation NavigationManager

    @synthesize presentationDuration, dismissalDuration, isPresenting;

    -(id)init{
        self = [super init];

        if(self){

            self.presentationDuration = 1.0;
            self.dismissalDuration = 0.5;
        }

        return self;
    }

    -(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{

        return self.isPresenting ? self.presentationDuration : self.dismissalDuration;
    }

    -(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{


        self.transitionContext = transitionContext;
        if(self.isPresenting){
            [self executePresentation:transitionContext];
        }
        else{

            [self executeDismiss:transitionContext];
        }

    }

    ...


    @end

And in class that should process implement transitions:

@interface ViewController : UIViewController <UIViewControllerTransitioningDelegate>

@property (nonatomic, strong) NavigationManager navigationManager;

..

@end

@implementation

...

//Here I`m doing navigation to new UIViewController

 - (void)navigateToNewScreen
{
    DetailedNewsController *secVC = [[DetailedNewsController alloc] init];
    secVC.fullDescription = fullDescription;
    secVC.headerImage = a.imageView.image;
    self.transitioningDelegate = self;
    [self.navigationController pushViewController:secVC animated:YES];
}

 - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source
{
    self.animationController.isPresenting = YES;
    return self.animationController;
}

 - (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.animationController.isPresenting = NO;
    return self.animationManager;
}

@end

Default animation of transitioning is performed. Also navigation controller bar is not displayed after navigation.

UPDATE:

I thing problem in way how I`m creating UINavigationController in AppDelegate:

firstVC = [[ViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:firstVC];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blackColor];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];

Upvotes: 0

Views: 3174

Answers (1)

mbo42
mbo42

Reputation: 760

I'll make an answer with the new information you have provided. The reason I make it an answer is that I just don't want to give you some copy paste code, but to try to give a brief explanation behind it. If I understand correctly, you now want to present a ViewController with a custom transition.

So you got the custom transition working by changing your code to this:

secVC.transitioningDelegate = self; 
secVC.modalPresentationStyle = UIModalPresentationCustom; 
[self presentViewController:secVC animated:YES completion:nil];

Since we got that up and running, what's currently missing is the NavigationBar of the ViewController you want to show. Since you're presenting a ViewController, it won't be contained within the existing NavigationController stack in which the the ViewController you're presenting is within.

Therefore, you need to wrap your VC you want to present within a UINavigationController.

[self presentViewController:[[UINavigationController alloc] initWithRootViewController:secVC] animated:YES completion:nil];

Upvotes: 1

Related Questions