iosswifttransparentside-menu

How to add transparent background while open the side menu in swift?


I am using left side menu from jonkykong/SideMenu. If I open side menu I need transparent background view and if i close then background should change to its original colour. For that i am trying to set alpha value for side menu.

I have tried two ways:

1) Here i have installed pod 'SideMenu' and added below code:

 import UIKit
 import SideMenu

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        sideMenuConfig()
    }
    func sideMenuConfig(){
        // Define the menus
        SideMenuManager.default.menuLeftNavigationController = storyboard!.instantiateViewController(withIdentifier: "UISideMenuNavigationController") as? UISideMenuNavigationController
        var set = SideMenuSettings()
        set.presentationStyle.presentingEndAlpha = 1
        SideMenuManager.default.menuPresentMode = .menuSlideIn
        SideMenuManager.default.menuFadeStatusBar  = false
        SideMenuManager.default.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar)
        SideMenuManager.default.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController!.view)
    }
} 

here why pod not recognising SideMenuSettings.

error: Use of unresolved identifier 'SideMenuSettings'

2) Updated pod to swift 5 pod 'SideMenu', '~> 6.0' and below code:

 import UIKit
 import SideMenu

 class ViewController: UIViewController, SideMenuNavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        menuSettings()
    }
    func menuSettings(){
        let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
        menu.blurEffectStyle = nil
        var set = SideMenuSettings()
        set.statusBarEndAlpha = 0
        set.presentationStyle = SideMenuPresentationStyle.menuSlideIn
        set.presentationStyle.presentingEndAlpha = 0.5
        set.menuWidth = min(view.frame.width, view.frame.height) * 0.90
        menu.settings = set
        SideMenuManager.default.leftMenuNavigationController = menu
    }
}

here background view also moving with side menu like below How add give alpha value to side menu in swift.

Please help with the side menu code.

enter image description here


Solution

  • Just create a subclass of SideMenuNavigationController and set the presentationStyle's backgroundColor and presentingEndAlpha properties in it. From now on, you can use your CustomSideMenuNavigationController as your SideMenuNavigationController.

    class CustomSideMenuNavigationController: SideMenuNavigationController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setNavigationBarHidden(true, animated: false)
    
            self.presentationStyle = .menuSlideIn
            self.presentationStyle.backgroundColor = .white
            self.presentationStyle.presentingEndAlpha = 0.7
    
            self.statusBarEndAlpha = 0.0
            self.menuWidth = (UIScreen.main.bounds.width / 5) * 4
        }
    
    }