-4

I am trying to implement side menu for my iOS app, using this library https://github.com/jonkykong/SideMenu By code implementation, below the code written in view did load func:

    let menuRightNavigationController = UISideMenuNavigationController(rootViewController: SideMenuViewController())

    SideMenuManager.default.menuRightNavigationController = menuRightNavigationController
    SideMenuManager.default.menuFadeStatusBar = false

    SideMenuManager.default.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar)
    SideMenuManager.default.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController!.view)

But the Application is crashing on last two lines of the above code, and the below code written in button action:

    @objc func menuButtonAction(sender: UIButton!) {
    present(SideMenuManager.default.menuRightNavigationController!, animated: true, completion: nil)    
}

Guys please help to handle the crashing so I can present my side menu. Thanks!

Huzaifa ameen
  • 143
  • 1
  • 8

2 Answers2

0

I'm pretty sure your app crashes when it runs into nil value in this line: SideMenuManager.default.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar), however without a crash log I can only make some assumptions.

I am assuming the self here refers to UIViewController class. According to documentation the navigationController property is The nearest ancestor in the view controller hierarchy that is a navigation controller. This property is nil if the view controller is not embedded inside a navigation controller.

I would recommend you to make sure you initiate the UINavigationController object before you force-unwrap it. Here is useful documentation link, which may help you get the concept of navigation controller.

grzebyk
  • 1,024
  • 1
  • 15
  • 26
0

You need to change your a line of code from this

SideMenuManager.default.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar)

to this

SideMenuManager.default.menuAddPanGestureToPresent(toView: self.view)
Helogale
  • 231
  • 2
  • 9