topLayoutGuide equivalent in UIPresentationController?

99 Views Asked by At

I have a custom transition for presenting a view controller and I use a UIPresentationController subclass to perform the presentation.

In the presentation controller I add a couple of subviews to its containerView (the chrome). I would like to constrain one of them with something like the topLayoutGuide in order to account for the height of any top bars (like the status bar).

However, UIPresentationController is not a UIViewController subclass and as such it doesn't have a topLayoutGuide property. I tried constraining the views with the presentingViewController's and the presentedViewController's topLayoutGuide but the app crashes because they are not part of the same view hierarchy.

So is there any way to position subviews inside the presentation controller's containerView at its top while still accounting for the height of any top bars?

1

There are 1 best solutions below

0
On

By trial and error I found out that for topLayoutGuide to work it is important that the added view controllers view height is not larger than the screen size height. In UIViewControllerAnimatedTransitioning in func animateTransition(using transitionContext: UIViewControllerContextTransitioning) set:

toVC.view.frame = transitionContext.finalFrame(for: toVC)