简体   繁体   中英

Swift SideMenu show view controller

So I'm having a table view controller and using https://github.com/jonkykong/SideMenu i'm trying to display a "slide in" sidebar which works, but it doesn't show me the view that I want in the sidebar is black

在此处输入图片说明

// Define the menus
let menuLeftNavigationController = UISideMenuNavigationController()
menuLeftNavigationController.leftSide = true
// UISideMenuNavigationController is a subclass of UINavigationController, so do any additional configuration of it here like setting its viewControllers.
SideMenuManager.menuLeftNavigationController = menuLeftNavigationController

// Enable gestures. The left and/or right menus must be set up above for these to work.
// Note that these continue to work on the Navigation Controller independent of the View Controller it displays!
SideMenuManager.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar)
SideMenuManager.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController!.view)

When It clicks on the sidebar button I have this, which creates the animation but doesn't show the viewcontroller

func someAction(){
    present(SideMenuManager.menuLeftNavigationController!, animated: true, completion: nil)
    debugPrint("clicked")
}

在此处输入图片说明

On the repo readme, you will find your answer, take a look on the Customization section https://github.com/jonkykong/SideMenu#sidemenumanager .

Simply set menuFadeStatusbar = false

SideMenuManager.default.menuFadeStatusBar = false

From the previous link, you will find the following information : "Draws the menuAnimationBackgroundColor behind the status bar. Default is true. If menuFadeStatusBar is true, this color is used to fade it. Default is black."

The answer is in the comment of the snippet you posted:

// UISideMenuNavigationController is a subclass of UINavigationController, 
// so do any additional configuration of it here like setting its viewControllers.
      let btnMenu:UIButton = UIButton()
    btnMenu.frame = CGRect(x: 20*valuePro, y: 20*valuePro, width: 40*valuePro, height: 40*valuePro)
    btnMenu.backgroundColor = .red
    btnMenu.addTarget(self, action: #selector(self.displayMenu), for: .touchUpInside)
    self.view.addSubview(btnMenu)
@objc func displayMenu(sender: UIButton!) {
    print("Button Clicked")
    present(SideMenuManager.default.menuLeftNavigationController!, animated: true, completion: nil)
}

https://github.com/jonkykong/SideMenu

The SideMenu README

Code Implementation

First:

import SideMenu

From a button, do something like this:

// Define the menu
let menu = UISideMenuNavigationController(rootViewController: YourViewController)
// UISideMenuNavigationController is a subclass of UINavigationController, so do any additional configuration 
// of it here like setting its viewControllers. If you're using storyboards, you'll want to do something like:
// let menu = storyboard!.instantiateViewController(withIdentifier: "RightMenu") as! UISideMenuNavigationController
present(menu, animated: true, completion: nil)

Answer

You need use UISideMenuNavigationController(rootViewController:) , not UISideMenuNavigationController() .

If you open the side menu from left, set leftSide to true .

let menu = UISideMenuNavigationController(rootViewController: YourViewController)
menu.leftSide = true
present(menu, animated: true, completion: nil)

Of course, you should give instance variable to UISideMenuNavigationController(rootViewController:) .

I can recommend using JASidePanels

It's pretty simple and it just works. You're creating an JASidePanelController , setting this class to empty viewcontroller in your Storyboard and making this controller initial. (do not forget to import JASidePanels at the top of the class)

Then, in this class, you're implementing awakeFromNib() method like this:

leftPanel = ..//instantiating menu controller
let centerController = ...//instantiating center controller
centerPanel = UINavigationController(rootViewController: centerController)

That's it. You can instantiate controller via their ID which can be set in Identity Inspector

let stb = UIStoryboard(name: "Main", bundle: nil) //instantiating a storyboard we will use for instantiating controllers
let someController = stb.instantiateViewController(withIdentifier: "here_your_identifier_goes") as! YourControllerClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM