1

I have a view controller that on certain action presents another view controller that covers bottom half of the screen.

When I animate the second view controller (presented view controller) from the bottom to cover the bottom half of the screen, I also want animate the presenting view to top half of the screen. See the code below -

- (void)animatePresentationWithTransitioningContext:(id<UIViewControllerContextTransitioning>) transitioningContext
{
    UIViewController *presentedController = [transitioningContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *presentedControllerView = [transitioningContext viewForKey:UITransitionContextToViewKey];
    UIView *containerView = [transitioningContext containerView];

    presentedControllerView.frame = [transitioningContext finalFrameForViewController:presentedController];

    CGPoint center = presentedControllerView.center;
    center.y = containerView.bounds.size.height;
    presentedControllerView.center = center;

    [containerView addSubview:presentedControllerView];

    // This returns nil
    UIView *presentingControllerView = [transitioningContext viewForKey:UITransitionContextFromViewKey];

    [UIView animateWithDuration:[self transitionDuration:transitioningContext] delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        CGPoint presentedViewCenter = presentedControllerView.center;
        presentedViewCenter.y -= containerView.bounds.size.height/2.0;
        presentedControllerView.center = presentedViewCenter;

        // This is where I want to move the presenting view controller.
        // of course it does not work.
        CGPoint presentingViewCenter = presentingControllerView.center;
        presentingViewCenter.y -= containerView.bounds.size.height/2.0;
        presentingControllerView.center = presentingViewCenter;

    } completion:^(BOOL finished) {
        [transitioningContext completeTransition:finished];
    }];
}

The viewForKey:UITransitionContextFromViewKey returns nil. I overrode shouldRemovePresentersView to return NO in my custom UIPresentationController but I still get nil. I am using UIModalPresentationCustom as the modal presentation style for the view that I am presenting.

EDIT:

I realized that I can achieve the same affect by animating the presenting view by overriding the presentationTransitionWillBegin method in my custom UIPresentationController. But I would like to get an answer on accessing the presenting view controller.

EDIT: Since I have found a solution, clarifying my question: (1) Is this the expected behavior i.e. the presentingViewController is going to be nil in the animator object?

Shirish Kumar
  • 1,532
  • 17
  • 23
  • You've proved it can't be done the first way and you know how to do it the second way, so it is unclear what you are asking at this point. You should answer your own question showing your solution, and move on. – matt May 19 '15 at 20:18
  • @matt see my edit above - "Since I have found a solution, clarifying my question: (1) Is this the expected behavior i.e. the presentingViewController is going to be nil in the animator object?" – Shirish Kumar May 20 '15 at 16:44
  • And I answered that. – matt May 20 '15 at 16:55

1 Answers1

1

I am using UIModalPresentationCustom as the modal presentation style for the view that I am presenting.

Well, there's your problem. In that case, the From view will be nil. It is nil unless you are using the FullScreen presentation style.

I'm not saying you are wrong to make the presentation style Custom. You need to do that, if you want to leave the presenting view controller's view in place, and if you want to supply your own presentation controller. And you do! But in that case, the work is divided: it is the presentation controller that becomes responsible for the final position of the view.

By the way, it occurs to me (thinking about it some more) that you are doing something you should probably not be doing. You are not expected to move the presenting view controller's view. It might be safer to take a snapshot view of the presenting view controller's view and animate that, behind your presented view controller's view.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • If you understand you correctly, you are suggesting to not use custom animator. correct? – Shirish Kumar May 19 '15 at 21:06
  • Not at all! Custom transition animations are my favorite feature of iOS 7/8. I'm just describing how the animator and the presentation controller work together. The work needs to be divided up properly, as I do here for example: https://github.com/mattneub/custom-alert-view-iOS7/blob/master/CustomAlertViewDemo/CustomAlertViewTransitioner.swift – matt May 19 '15 at 21:59
  • May be using presentation controller is not the right way to solve the problem that I am trying to solve. I want to have access to both presenting controller and presented controller - user should be able to interact with both of them (say, scroll in one, add text in another). I realized that when the presented controller is presented user cannot interact with the presenting controller. Not sure if that is always the case, if true, I should try some other approach. – Shirish Kumar May 20 '15 at 16:57
  • "user should be able to interact with both of them" Then you should never have been using a presented view controller in the first place, since interaction with a different view controller is totally impossible. You should simply use a view sliding over another view. – matt May 20 '15 at 17:00
  • Or, if you really want to use a second view controller, then create your own custom container (parent) view controller so that it can display the child view controller's view and its own view simultaneously, similar to what a split view controller does. – matt May 20 '15 at 17:19