Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 867
Doug Smith Avatar asked Apr 02 '15 22:04

Doug Smith


People also ask

What is presentation controller?

The presentation controller's role during all of these phases is to manage its own custom views and state information. During the presentation and dismissal phases, the presentation controller adds its custom views (if any) to the view hierarchy and creates any appropriate transition animations for those views.


1 Answers

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.

like image 58
egdmitry Avatar answered Sep 23 '22 15:09

egdmitry