简体   繁体   English

Swift SideMenu 显示视图控制器

[英]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所以我有一个表视图控制器并使用https://github.com/jonkykong/SideMenu我试图显示一个有效的“幻灯片”侧边栏,但它没有显示我想要的视图侧边栏是黑色的

在此处输入图片说明

// 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 .在 repo 自述文件中,您会找到答案,请查看自定义部分https://github.com/jonkykong/SideMenu#sidemenumanager

Simply set menuFadeStatusbar = false只需设置 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."从上一个链接中,您会发现以下信息:“在状态栏后面绘制menuAnimationBackgroundColor 。默认为true。如果menuFadeStatusBar为true,则使用该颜色对其进行淡化。默认为黑色。”

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 https://github.com/jonkykong/SideMenu

The SideMenu README 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() .您需要使用UISideMenuNavigationController(rootViewController:) ,而不是UISideMenuNavigationController()

If you open the side menu from left, set leftSide to true .如果从左侧打开侧边菜单,请将leftSide设置为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:) .当然,你应该给UISideMenuNavigationController(rootViewController:)提供实例变量。

I can recommend using JASidePanels我可以推荐使用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.您正在创建一个JASidePanelController ,将此类设置为 Storyboard 中的空视图控制器,并使该控制器初始。 (do not forget to import JASidePanels at the top of the class) (不要忘记在类的顶部import JASidePanels

Then, in this class, you're implementing awakeFromNib() method like this:然后,在这个类中,您将实现像这样的awakeFromNib()方法:

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您可以通过他们的 ID 实例化控制器,该 ID 可以在 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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