Why doesn't defining my view controller size with frameOfPresentedViewInContainerView make it that size?
Asked Answered
M

2

12

I've been following this tutorial on implementing custom view controller transitions in iOS 8 with UIPresentationController, and so far it all makes sense, but I can't seem to get my view controller to be the right size.

In that tutorial, they have the following code:

class OverlayPresentationController: UIPresentationController {
   let dimmingView = UIView()

  override init(presentedViewController: UIViewController!, presentingViewController: UIViewController!) {
    super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    dimmingView.backgroundColor = UIColor(white: 0.0, alpha: 0.5)
  }

  override func presentationTransitionWillBegin() {
    dimmingView.frame = containerView.bounds
    dimmingView.alpha = 0.0
    containerView.insertSubview(dimmingView, atIndex: 0)

    presentedViewController.transitionCoordinator()?.animateAlongsideTransition({
      context in
      self.dimmingView.alpha = 1.0
    }, completion: nil)
  }

  override func dismissalTransitionWillBegin() {
    presentedViewController.transitionCoordinator()?.animateAlongsideTransition({
      context in
      self.dimmingView.alpha = 0.0
    }, completion: {
      context in
      self.dimmingView.removeFromSuperview()
    })
  }

  override func frameOfPresentedViewInContainerView() -> CGRect {
    return containerView.bounds.rectByInsetting(dx: 30, dy: 30)
  }

  override func containerViewWillLayoutSubviews() {
    dimmingView.frame = containerView.bounds
    presentedView().frame = frameOfPresentedViewInContainerView()
  }
}

I understand all of it, except for frameOfPresentedViewInContainerView. That returns a size, but, if I remove presentedView().frame = frameOfPresentedViewInContainerView() in containerViewWillLayoutSubviews it doesn't work. Why do I have to have that line? You would think the function itself would be sufficient, otherwise I'd just implement a random size in the containerViewWillLayoutSubviews method.

Muttra answered 2/4, 2015 at 22:3 Comment(0)
S
10

The frameOfPresentedViewInContainerView is used by UIKit to get the initial presentedView's frame that is then passed to an animator (conforming to UIViewControllerAnimatedTransitioning protocol) so that it knows the target position of the presented view when setting up an animation. After the presenting animation finishes and the presentation is up the screen or one of the parent view controllers may still resize because of a rotation or a size class change. UIPresentationController instance has a chance to respond to these changes in containerViewWillLayoutSubviews method and resize the presentedView appropriately.

In other words the presentation controller is always responsible for determining the layout for the presented view, but initially it just tells UIKit what the frame is so that the animator can use this information but after that the presentation controller sets the frame on the presented view directly.

Septuagenarian answered 13/7, 2016 at 1:20 Comment(1)
Thanks. I wondering what the difference was between the UIPresentationController and UIViewControllerAnimatedTransitioning protocols were.Vientiane
W
0

You only need to have that line if you want the presented view frame to be updated when the screen rotates.

Wilmawilmar answered 28/2, 2016 at 23:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.