I have some custom modal presentation and custom controller to present (subclass of UIViewController). It is it's own transitioning delegate and returns some animated transitioning object and presentation controller. I use animated transitioning object to add presented view to container view when presenting and to remove it when dismissing, and of course to animate it. I use presentation controller to add some helper subview.
public final class PopoverPresentationController: UIPresentationController {
private let touchForwardingView = TouchForwardingView()
override public func presentationTransitionWillBegin() {
super.presentationTransitionWillBegin()
self.containerView?.insertSubview(touchForwardingView, atIndex: 0)
}
}
public final class PopoverAnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning {
func setupView(containerView: UIView, presentedView: UIView) {
//adds presented view to container view
}
public func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
//1. setup views
//2. animate presentation or dismissal
}
}
public class PopoverViewController: UIViewController, UIViewControllerTransitioningDelegate {
init(...) {
...
modalPresentationStyle = .Custom
transitioningDelegate = self
}
public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return PopoverAnimatedTransitioning(forPresenting: true, position: position, fromView: fromView)
}
public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return PopoverAnimatedTransitioning(forPresenting: false, position: position, fromView: fromView)
}
public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController?, sourceViewController source: UIViewController) -> UIPresentationController? {
return PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting, position: position, fromView: fromView)
}
}
Everything works fine when I present the controller with presentViewController
and pass true in animated
property. But when I want to present it without animation and pass false, UIKit only calls presentationControllerForPresentedViewController
method, and does not call animationControllerForPresentedController
at all. And as far as presented view is added to views hierarchy and positioned in it in animation transitioning object, which is never created, nothing is presented.
What I'm doing is I'm checking in presentation controller if transition is animated and if not I create animated transitioning object manually and make it to setup views.
override public func presentationTransitionWillBegin() {
...
if let transitionCoordinator = presentedViewController.transitionCoordinator() where !transitionCoordinator.isAnimated() {
let transition = PopoverAnimatedTransitioning(forPresenting: true, position: position, fromView: fromView)
transition.setupView(containerView!, presentedView: presentedView()!)
}
}
It works but I'm not sure if that's the best approach.
Documentation says that presentation controller should be responsible only for doing any additional setup or animation during transition and the main work for presentation should be done in animated transitioning object.
Is it ok to always setup views in presentation controller instead and only animate them in animated transitioning object?
Is there any better way to solve that problem?
Solved that by moving all the logic of views setup from animated transitioning to presentation controller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With