【问题标题】:swift: change the color of the navigation barswift:更改导航栏的颜色
【发布时间】:2021-01-01 01:39:59
【问题描述】:

我正在使用带有红色导航栏的FirstViewController。当我转到下一个 SecondViewController 时,我使用此代码清除导航栏颜色:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)

但是当我回到FirstViewController 时,我的导航栏颜色不是红色。很明显。但它应该是红色的。如何解决?

FirstViewController中的代码:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationItem.largeTitleDisplayMode = .always
        self.navigationController?.navigationBar.shadowImage = UIImage()
        
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 239/255, green: 210/255, blue: 166/255, alpha: 1.0)
    }

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationbar


    【解决方案1】:

    代替barTintColor,使用backgroundColor来改变navigationBar'r的颜色,即

    FirstVC

    class FirstVC: UIViewController {
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
            self.navigationController?.navigationBar.prefersLargeTitles = true
            self.navigationItem.largeTitleDisplayMode = .always
            self.navigationController?.navigationBar.shadowImage = UIImage()
            self.navigationController?.navigationBar.backgroundColor = .red //here...
        }
    }
    

    SecondVC

    class SecondVC: UIViewController {
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            self.navigationController?.navigationBar.backgroundColor = .clear //here...
        }
    }
    

    【讨论】:

    • 有一个问题...在FirstViewController 中它是一个TableViewController,我需要navbar 在大模式下清晰,但是如果我向下滚动我的tableView,我需要导航栏显示为红色颜色。这不适用于navigationBar.backgroundColor
    • 这需要添加代码来管理基于滚动的颜色。单独添加 barTintColor 或 backgroundColor 并不能解决问题。
    • 我不知道为什么,但是如果我在我的问题中使用代码,这就是它的工作原理。滚动行为没有额外的代码。你能提供一些关于在哪里添加或滚动行为代码的提示吗?也许已经有类似的问题和答案?
    • 您需要在您的FirstVC 中实现scrollViewDidScroll(_:) 方法。确定滚动方向,然后根据它更改navigationBar's 颜色。
    • 小澄清。你确定你在`SecondVC`中的navigation bar真的是透明的而不是白色的吗?因为在我的应用程序中,我使用图像作为背景,如果我写这个navigationBar.backgroundColor/tintColor/tintBackgroundColor .clear ,那么我只会在navbar 区域得到黑色
    【解决方案2】:

    编辑您的问题。目前尚不清楚您希望导航栏是透明的还是红色的。
    但是,您可以将其放入 AppDelegate

    let mainVC = MainViewController()
    let navigationController = UINavigationController(rootViewController: mainVC)
    navigationController.navigationBar.barTintColor = .clear// or .red
    

    【讨论】:

    • 我写的很清楚。在FirstViewController 红色。在SecondViewController 清除颜色。
    • 好的,然后把标题从swift: change the color of the navigation bar to clear改成swift: change the color of the navigation bar:)
    • 但是您的代码没有帮助。如果我使用navigationController.navigationBar.barTintColor = .clear,我会在导航栏中看到半透明的深色背景。
    • 好的,也尝试添加这一行:navigationController.navigationBar.isTranslucent = true
    【解决方案3】:

    ViewWillAppear方法中设置FirstViewController导航栏的颜色。

    【讨论】:

    • 已经。但这并没有帮助。在我的问题中添加来自FirstViewController 的代码。
    • 我不知道我要说什么,但我想问题是因为“prefersLargeTitles”和“largeTitleDisplayMode”。临时 cmets 这两行代码,你看就可以正常工作了!!
    猜你喜欢
    • 2014-09-01
    • 2016-06-28
    • 2015-11-03
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多