Learn Courses My Dashboard

Question about custom Navigation Bar background color

Hi, I use the following code to change the Navigation Bar background color. But I don’t know how to use my custom color instead of using UIColor only. Does anyone has any suggestion? Thank you.

    init() {
        let coloredAppearance = UINavigationBarAppearance()
        coloredAppearance.backgroundColor = .yellow
        coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
        UINavigationBar.appearance().standardAppearance = coloredAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance

I’m not sure what you mean. What is your custom color? Do you mean that your custom colour is not in UIColor format?

I add a new color set to Assets folder and name it “bgcolor”. But If I change coloredAppearance.backgroundColor = .yellow to .bgcolor or Color(Color(“bgcolor”)), it will not accept the code. Therefore, I would like to know if there has any way to use my custom color to change the Navigation Bar background.

I haven’t had experience with adding a colour set to the Assets folder and then accessing them, but someone else here probably knows. Another approach would be to add your colour directly. Colour pickers are very helpful in that regard. Just hover over the colour that you want to use and it will show you the code for it, usually in UIColor, RGB and HEX.

Assuming this is for SwiftUI (given you tried Color(Color(“bgcolor”))), you would access a color from your asset catalog like so:


But since you are trying to assign it to a property that actually takes a UIColor, you need to do it like this:

UIColor(named: "bgcolor")

Thanks a lot. It works!!