0

Here's what I am tring to do:

Implement a side nav that will slide in with custom animation from left to right

I am refering to this: https://www.thorntech.com/2016/03/ios-tutorial-make-interactive-slide-menu-swift/

But this and all other tutorials that implement this functionality use segues to present the view.

So their code goes here:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
        if let destinationViewController = segue.destinationViewController as? MenuViewController {
            destinationViewController.transitioningDelegate = self
        }
} 

But this is not an option for me!

My entire view controller is created by code. Right now it's a dummy view controller and I am just trying to get it to slide in from left to right.

import UIKit

class SideNavVC: UIViewController {

    static let sideNav = SideNavVC()

    override func viewDidLoad() {
        super.viewDidLoad()

        let dummyView = UIView(frame: CGRect(x: 10, y: 200, width: 100, height: 100))
        dummyView.backgroundColor = accentColor

        view.addSubview(dummyView)

        let closeBtn = UIButton(frame: CGRect(x: 4, y: 30, width: 200, height: 20))
        closeBtn.addTarget(self, action: #selector(closeViewTapped), for: .touchUpInside)
        closeBtn.setTitle("Close", for: .normal)

        view.addSubview(closeBtn)

        view.backgroundColor = confirmGreen
    }

    @objc func closeViewTapped(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
}

And I am stuck on this step of the tutorial: https://www.thorntech.com/2016/03/ios-tutorial-make-interactive-slide-menu-swift/#four

This is how I am trying to set the delegate to self:

func addSlideGesture() {

    let edgeSlide = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(presentSideNav))
    edgeSlide.edges = .left
    view.addGestureRecognizer(edgeSlide)
}

@objc func presentSideNav() {

    if presentedViewController != SideNavVC.sideNav {
        SideNavVC.sideNav.transitioningDelegate = self
        SideNavVC.sideNav.modalPresentationStyle = .custom
        self.present(SideNavVC.sideNav, animated: true, completion: nil)
    }
}

This is where I was implementing my delegate (MainVC is from where the user will slide right)

extension MainVC: UIViewControllerTransitioningDelegate {
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        print("Source: \(source)")
        print("Destination \(presenting)")

        if source == self && presenting == SideNavVC.sideNav {
            print("PRESENTING SIDE NAV")
            return RevealSideNav()
        }

        return nil
    }

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        //Added a break point here. It is hitting.
        return nil
    }

}

Since my delegate was not calling, I decided to add presentationController(forPresented,presenting,source) and put a break point in there which was hitting. So I suspect that I am missing some part of the code that needs to go in there.

When I googled it, I found this UIPresentationController tutorial https://www.raywenderlich.com/915-uipresentationcontroller-tutorial-getting-started

But as I went through it, the more and more it started confusing me.

I am sure there is some small part that I am missing. Because if it works when the delegte is set in prepare for segue, then it should also work when calling present(_,animated,completion)

Can someone point me in right direction? How do I get the custom animations to trigger and the delegate methods to call? Does anyone know a tutorial that teaches how to do this using just code and no storyboard?

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
Parth
  • 2,682
  • 1
  • 20
  • 39

1 Answers1

0

I referred to answer by @Womble

"UIViewControllerTransitioningDelegate method not called in iOS 7"

Turns out the method that I was using was wrong and Xcode didn't warn of this. Kinda odd.

 func animationController(forPresented presented: UIViewController,
                             presenting: UIViewController,
                             source: UIViewController)
        -> UIViewControllerAnimatedTransitioning?
    {
        print("Source: \(source)")
        print("Destination \(presenting)")
        // ... return your cached controller object here.
        return RevealSideNav()
    }
Parth
  • 2,682
  • 1
  • 20
  • 39