create uipresentation contoller in swift

To create a UIPresentationController in Swift, follow the steps below:

  1. Create a new class and inherit from UIPresentationController.
main.swift
class MyPresentationController: UIPresentationController {
    // write your code here
}
89 chars
4 lines
  1. Implement the required and optional methods of UIPresentationController protocol as per your requirements. Some of the important methods are:

    • presentationTransitionWillBegin(): This method gets called when the presentation animation is about to start. Use this method to set up any views or animations required for the presentation.
    main.swift
     override func presentationTransitionWillBegin() {
         guard let containerView = containerView else { return }
    
         // Add a dimming view behind the presented view controller
         dimmingView.frame = containerView.bounds
         dimmingView.alpha = 0
         containerView.insertSubview(dimmingView, at: 0)
    
         // Animate the dimming view alpha
         if let coordinator = presentingViewController.transitionCoordinator {
             coordinator.animate(alongsideTransition: { _ in
                 self.dimmingView.alpha = 0.5
             })
         } else {
             dimmingView.alpha = 0.5
         }
     }
    
    586 chars
    18 lines
    • dismissalTransitionWillBegin(): This method gets called when the dismissal animation is about to start. Use this method to set up any views or animations required for the dismissal.
    main.swift
    override func dismissalTransitionWillBegin() {
         if let coordinator = presentingViewController.transitionCoordinator {
             coordinator.animate(alongsideTransition: { _ in
                 self.dimmingView.alpha = 0
             })
         } else {
             dimmingView.alpha = 0
         }
     }
    
    286 chars
    10 lines
    • size(forChildContentContainer:withParentContainerSize:): This method is used to provide the size of the presented view controller.
    main.swift
     override var frameOfPresentedViewInContainerView: CGRect {
         guard let containerView = containerView,
               let presentedView = presentedView else { return .zero }
    
         let containerViewBounds = containerView.bounds
         let presentedContentSize = size(forChildContentContainer: presentedViewController,
                                          withParentContainerSize: containerViewBounds.size)
         var presentedViewFrame = containerViewBounds
         presentedViewFrame.size.height = presentedContentSize.height
         presentedViewFrame.origin.y = containerViewBounds.maxY - presentedContentSize.height
         presentedView.frame = presentedViewFrame
         return presentedViewFrame
     }
    
     override func size(forChildContentContainer container: UIContentContainer, withParentContainerSize parentSize: CGSize) -> CGSize {
         return CGSize(width: parentSize.width, height: 300)
     }
    
    882 chars
    18 lines
  2. Use the presentation controller in your presenting view controller.

main.swift
let presentedVC = // instantiate your presented view controller
presentedVC.modalPresentationStyle = .custom
presentedVC.transitioningDelegate = self // the presenting view controller should conform to UIViewControllerTransitioningDelegate
present(presentedVC, animated: true, completion: nil)
294 chars
5 lines
  1. Conform to the UIViewControllerTransitioningDelegate protocol in your presenting view controller and implement the following methods:
main.swift
extension MyPresentingViewController: UIViewControllerTransitioningDelegate {
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return MyPresentationController(presentedViewController: presented, presenting: presenting)
    }

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return // create and return an instance of your animated transition class for presentation
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return // create and return an instance of your animated transition class for dismissal
    }
}
843 chars
14 lines

That's it, you have successfully created a custom UIPresentationController in Swift.

gistlibby LogSnag