ios - 如何在快速打开侧边菜单时添加透明背景?

标签 ios swift transparent side-menu

我正在使用来自 jonkykong/SideMenu 的左侧菜单.如果我打开侧面菜单,我需要透明背景 View ,如果我关闭,那么背景应该变成原来的颜色。为此,我正在尝试为侧边菜单设置 alpha 值。

我试过两种方法:

1) 我在这里安装了 pod 'SideMenu' 并添加了以下代码:

 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)
    }
} 

这里是 pod 无法识别 SideMenuSettings 的原因。

error: Use of unresolved identifier 'SideMenuSettings'

2) 将 pod 更新为 swift 5 pod 'SideMenu', '~> 6.0' 和以下代码:

 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
    }
}

这里的背景 View 也随着侧面菜单移动,如下所示 如何在 swift 中为侧边菜单添加赋予 alpha 值。

请帮助提供侧边菜单代码。

enter image description here

最佳答案

只需创建 SideMenuNavigationController 的子类并在其中设置 presentationStyle 的 backgroundColorpresentingEndAlpha 属性。从现在开始,您可以将 CustomSideMenuNavigationController 用作 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
    }

}

关于ios - 如何在快速打开侧边菜单时添加透明背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59932928/

相关文章:

ios - Swift 中的 "do-catch"语句执行四次?

ios - 从编程制作的 Controller 中呈现 Storyboard Controller

ios - 使图像在swift中没有可触摸的框架

python - 使用 pygame 在 python 中使图像背景透明的问题

ios - 我们如何使用 swift 在 Collection View 上添加按钮?

ios - Scenekit中如何将一个SCNNode拆分成多个节点?

iphone - 如何同时播放iPod库中的两个音频文件?

sql - 选择sqlite(Swift)中的count(col_name)不起作用

html - 在滚动条后面扩展一个元素

HTML/CSS 覆盖透明框