iosswiftswrevealviewcontroller

SWRevealViewController add gesture to a presented viewcontroller


I am using SWRevealViewController in my project, it is working perfectly, the TableView has 6 options, each pressing leads to a different ViewController.

The problem comes when I instantiate another ViewController manually (called viewControllerModelos), this viewcontroller is not linked to a side menu option, but I do need that from that viewcontroller it can also be displayed.

I present the ViewController instantiated correctly, but the Side Menu does not appear when sliding, although I have the gesture added in that ViewController

Could someone tell me the way please?

The code below is in a viewcontroller in which the side menu is shown

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewControllerModelos = storyBoard.instantiateViewController(withIdentifier: "viewControllerModelos") as! VC_catalogo_modelos
        self.present(viewControllerModelos, animated: true, completion: nil)

The code below is that of the instantiated viewcontroller

import Foundation
import UIKit

class VC_catalogo_modelos: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if revealViewController() != nil {
            self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
        }   
    }
}

Solution

  • Currently you are just presenting VC_catalogo_modelos you are not settings root to revealViewController due to this reason nothing is working in your case

       let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let sw = storyboard.instantiateViewController(withIdentifier: "sw") as! SWRevealViewController // can place sw with revealViewCOntroller identifier
                self.view.window?.rootViewController = sw
                let viewControllerModelos = storyBoard.instantiateViewController(withIdentifier: "viewControllerModelos") as! VC_catalogo_modelos
                let navigationController = UINavigationController(rootViewController: viewControllerModelos!) // using navigationController can help in future if there are other controller on action 
                navigationController.navigationBar.isHidden=false // unhide if you need navigationController bar
                navigationController.setNavigationTints()
    
                 sw.setFront(navigationController, animated: true)
                //pushFrontViewController(navigationController, animated: true) Use only if you require a push otherwise setFront