0

In my code I am using the present(:animated:completion:) method to present ViewController2 from ViewController1 that is the root view controller of a container view nested within RootViewController.

I've set the presentation style to be .coverVertical and I have ViewController1's definesPresentationContext variable set to true in it's viewDidLoad() method. I also have ViewController2's presentation set to .overCurrentContext. This makes ViewController2's bounds the same as ViewController1's but for whatever reason the .coverVertical animation starts at the bottom of the screen instead of starting from the bottom of ViewController1's frame.

However, this goes away when I set the root view controller of the container view to be a UINavigationController and nest ViewController1 within it. I assume this means that there is some second context variable that I'm missing that prevents the animations from animating overtop other views, but I couldn't seem to find any other variables besides definesPresentationContext.

Brinkster
  • 182
  • 1
  • 11
  • There is no such thing as UIViewController `definesCurrentContext` variable. Is this something you just made up? – matt Jul 30 '18 at 20:11
  • https://developer.apple.com/documentation/uikit/uiviewcontroller/1621456-definespresentationcontext Meant this variable, will edit OP – Brinkster Jul 30 '18 at 20:22

1 Answers1

2

So you are saying that your view controller hierarchy is

RootViewController
    ViewController1

In that case, run this code inside ViewController1:

let vc = // ViewController2 instance, obtained somehow
vc.modalTransitionStyle = .coverVertical
self.definesPresentationContext = true
vc.modalPresentationStyle = .currentContext
self.present(vc, animated: true)

You will see that only the area of ViewController1's view is involved in the transition.

Note that the clipsToBounds of the container view must be set to true. If there is no container view, add one (to provide the clipping) — though I believe, from your description, that there is one (i.e. that you configured this in a storyboard).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yeah, but change it to `let vc = // ViewController2 instance, obtained somehow` `vc.modalTransitionStyle = .coverVertical` `self.definesPresentationContext = true` `vc.modalPresentationStyle = .currentContext` `self.present(vc, animated: true)` And it ends in the correct spot but the coverVertical animation starts at the bottom of the screen, atleast for me – Brinkster Jul 30 '18 at 20:24
  • Because you didn't turn on the `clipsToBounds` of the container view. I'll add that to my answer. – matt Jul 30 '18 at 20:56
  • But a custom transition animation would give you more control. Basically you don't like the default `coverVertical` animation and now you're trying to hack around it, when you might so easily just replace it with any animation of your own. – matt Jul 30 '18 at 21:55
  • So the end goal is to use a custom animation, but I just want the custom animation to take place within the bounds of ViewController1. Naturally, the first step to this is getting a native animation to work properly, and that's what this was. In the custom animation I want to present, the transitionContext that is provided in the UIViewControllerAnimatedTransitioning class should only be able to let me place UIViews within the bounds of ViewController1. I have a feeing that this doesn't resolve that core issue because clipsToBounds just kinda makes all the bad parts invisible, but I'm not sure – Brinkster Jul 30 '18 at 22:04
  • how to properly provide the transitionContext with the context it needs for the `viewFor(key: .to)` to have a point (0, 0) at the upperLeftCorner of ViewController1 – Brinkster Jul 30 '18 at 22:07
  • We have strayed completely out of the realm of the original question, which was about `.coverVertical`. If you are now writing a custom animation and you want to ask about it, ask a new question! Do not "leech" or "churn" in the comments. Thanks – matt Jul 30 '18 at 22:14