【问题标题】:How to add transparent background while open the side menu in swift?如何在快速打开侧边菜单时添加透明背景?
【发布时间】:2020-05-12 22:25:41
【问题描述】:

我正在使用来自jonkykong/SideMenu 的左侧菜单。如果我打开侧边菜单,我需要透明背景视图,如果我关闭,则背景应更改为其原始颜色。为此,我正在尝试为侧边菜单设置 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。

错误:使用未解析的标识符“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
    }
}

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

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

【问题讨论】:

  • 试试 - SideMenuSettings.presentationStyle.presentingEndAlpha = 1。
  • @Aditya,错误Use of unresolved identifier 'SideMenuSettings' ..请帮忙
  • 更改SideMenuSettings().presentationStyle.presentingEndAlpha = 1
  • @Aditya,同样的错误Use of unresolved identifier 'SideMenuSettings'
  • 是的,在你的 podfile 中添加 pod 'SideMenu' 来安装最新的 pods

标签: ios swift transparent side-menu


【解决方案1】:

只需创建一个 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
    }

}

【讨论】:

    【解决方案2】:

    对于透明背景需要更改 menuAnimationFadeStrength 的值。

    • 演示::

    SideMenuManager.default.menuAnimationFadeStrength = 0.5

    【讨论】:

    • 谢谢你,它非常适合选项一...你试过第二个选项更新 swift 5 pod 'SideMenu', '~> 6.0' 中的 pod
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多