I am trying to present a view controller modally. It is like a pop up view controller.
I am using animateTransition method to animate the transition. This is my code:
extension PopUpViewController: UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.25
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromVC = transitionContext.viewController(forKey: .from) else { return}
guard let toVC = transitionContext.viewController(forKey: .to) else { return}
guard let fromView = transitionContext.view(forKey: .from) else { return}
guard let toView = transitionContext.view(forKey: .to) else { return}
var containerView = transitionContext.containerView
if toVC == self {
// presenting
containerView.addSubview(toView)
toView.frame = fromView.frame
popUpView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
toView.alpha = 0
UIView.animate(withDuration: 0.25, animations: {
toView.alpha = 1
self.popUpView.transform = CGAffineTransform.identity
}) { _ in
DispatchQueue.main.async {
transitionContext.completeTransition(true)
}
}
} else {
// dismissing
UIView.animate(withDuration: 0.25, animations: {
self.popUpView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
fromView.alpha = 0
}) { _ in
transitionContext.completeTransition(true)
}
}
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
}
I am presenting this view controller (Presented VC) over the other view controller(Presenting VC) in this way. I am using the default modal presentation style. Initially I was using custom but I looked at some posts on StackOverflow and they said to remove it but still, I am getting a black screen(presenting VC) and I can see the presented VC over it.
When I am dismissing the presented VC, again the presenting VC is visible.
Please do let me know in case you need more details.
As per code when transitionContext.completeTransition(true)
is executed, presenting VC screen turns black