【问题标题】:Manually switch NavigationBar to Dark Mode手动将 NavigationBar 切换到深色模式
【发布时间】:2020-05-07 20:49:52
【问题描述】:

在我的应用程序中,我有一个手动切换,可以在亮/暗模式之间切换,我想要完成的是让导航栏触发“暗模式”外观(白色文本/图标和黑色背景)当我需要在明暗之间切换时。

我已经尝试了以下所有方法:

UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor=UIColor.white

self.navigationController?.navigationBar.barTintColor=UIColor.white
self.navigationController?.navigationBar.titleTextAttributes=[NSAttributedString.Key.foregroundColor:UIColor.white]

当输入我尝试过的任何代码时,导航栏永远不会改变。

实现此目的的正确方法是什么?

【问题讨论】:

    标签: swift uinavigationbar ios-darkmode


    【解决方案1】:

    没有办法通过一条简单的线路将栏更改为您自己的本地暗模式。但是我们可以编写一个函数来完成与您想要的类似的事情。请注意,执行此类操作的正确方法是添加一个使用 trait 集合在不同全局视觉模式之间切换的条形样式和颜色的开关。

    extension UIViewController {
    
        enum NavigationBarStyle {
            case dark, light
        }
    
        func setNavigationBar(style: NavigationBarStyle) {
    
            guard let bar = view.subviews.first(where: { return $0 is UINavigationBar }) as? UINavigationBar else { return }
    
            func set(item: UINavigationItem, color: UIColor) {
                item.rightBarButtonItem?.tintColor = color
                item.leftBarButtonItem?.tintColor = color
            }
    
            bar.barStyle = style == .dark ? .black : default
    
            let color: UIColor = style == .dark ? .white : .black
            for item in bar.items ?? [] {
                bar.titleTextAttributes = [.foregroundColor: color]
                set(item: item, color: color)
            }
    
        }
    
    }
    

    您需要确保已将导航栏添加到控制器的子视图中。我以编程方式做了类似的事情,但我想使用界面构建器是一样的。

    let navbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: 375, height: 45))
    
    navbar.backgroundColor = UIColor.white
    navbar.delegate = self
    
    let navItem = UINavigationItem()
    navItem.title = "Title"
    navItem.leftBarButtonItem = UIBarButtonItem(title: "Left", style: .plain, target: self, action: nil)
    navItem.rightBarButtonItem = UIBarButtonItem(title: "Right", style: .plain, target: self, action: nil)
    
    navbar.setItems([navItem], animated: true)
    
    view.addSubview(navbar)
    

    最后,在一些动作或任何你喜欢的东西之后设置样式。使用;

    setNavigationBar(style: .dark)
    

    【讨论】:

      【解决方案2】:

      在将我的头撞到墙上一段时间后,我终于解决了这个问题(这可能与您遇到的问题不同,但希望这对某人有所帮助)。 我将导航栏色调设置为“白色”。要在启用暗模式时自动更改导航栏颜色,需要将其设置为“默认”。

      【讨论】:

        【解决方案3】:

        这就是最终对我有用的东西:

        override var preferredStatusBarStyle: UIStatusBarStyle {
            return .lightContent
        }
        
        override func viewDidAppear(_ animated: Bool) {
            navigationController?.navigationBar.barStyle = .black
        }
        

        【讨论】:

        • 你错过了super.viewDidAppear(animated)
        猜你喜欢
        • 2023-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-10
        相关资源
        最近更新 更多