Search code examples
iosswiftuinavigationcontroller

ENSideMenuNavController - Disable Interaction with All Other Views


I have a Nav View which when it pops out by tapping the burger menu then opens to half the page. It still has the other views (the root view controller and its children) in the background.

When a user taps the greyed out background area where the other views are they can interact with those views and the page navigates leaving the nav view over the top.

The Nav View is used in multiple places so I need the code for disabling interactions with other views to be in the Nav View Controller. Code below.

import UIKit

class MenuNavViewController: ENSideMenuNavigationController, ENSideMenuDelegate {

    var tabBar: ManagerTabViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        let sb = UIStoryboard(name: "iPhoneStoryboard", bundle: nil)
        let menu = sb.instantiateViewController(withIdentifier: "MenuTableViewController") as! MenuTableViewController
        menu.tabBar = self.tabBar
        sideMenu = ENSideMenu(sourceView: self.view, menuViewController: menu, menuPosition: .left)
        sideMenu?.bouncingEnabled = false

        view.bringSubview(toFront: navigationBar)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // MARK: - ENSideMenu Delegate
    func sideMenuWillOpen() {
    }

    func sideMenuWillClose() {
    }

    func sideMenuDidClose() {

    }

    func sideMenuDidOpen() {

    }
}

How can I disable/enable interaction with other views (or at least the view it's launched from) in the NavViewController above?


Solution

  • I found the answer shown in the code below.

    Looking further into the ENSideMenuNavigationController class I found that the "init" method sets the relevant view controllers on "viewControllers" and implemented the code below which gave the desired effect.

    import UIKit
    
    class MenuNavViewController: ENSideMenuNavigationController, ENSideMenuDelegate {
    
        var tabBar: ManagerTabViewController!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let sb = UIStoryboard(name: "iPhoneStoryboard", bundle: nil)
            let menu = sb.instantiateViewController(withIdentifier: "MenuTableViewController") as! MenuTableViewController
            menu.tabBar = self.tabBar
            sideMenu = ENSideMenu(sourceView: self.view, menuViewController: menu, menuPosition: .left)
            sideMenu?.bouncingEnabled = false
            sideMenu?.delegate = self
    
    
            view.bringSubview(toFront: navigationBar)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
        // MARK: - ENSideMenu Delegate
        func sideMenuWillOpen() {
    
        }
    
        func sideMenuWillClose() {
        }
    
        func sideMenuDidClose() {
            // Enable interaction with other views again
            for viewController in self.viewControllers {
                viewController.view.isUserInteractionEnabled = true
            }
        }
    
        func sideMenuDidOpen() {
            // Disable interaction with other views
            for viewController in self.viewControllers {
                viewController.view.isUserInteractionEnabled = false
            }
        }
    }
    

    Any other suggestions for better practice/code would still be appreciated.