0

I've been trying for several hours to change the color of the Navigation Bar in my app... It's a little better than before, because I added the "isTranslucent" parameter, so now the Navigation Bar is gray (before this, it was completely white), but I still can't set it to the same color as the sidebar, and especially with the same height: the height becomes the same only when I press an element in the sidebar...

Here's what happens before I tap on a sidebar element:

Before tap

And here after I tapped it:

After tap

Here's the code:

import SwiftUI

let furl = URL(fileURLWithPath: "path")

struct ContentView: View {
    
    @State private var selected = 0
    @State private var isActive = false
    
    var body: some View {
        TabView(selection: $selected) {
            NavigationView {
                HomeView()
            }
            .navigationViewStyle(StackNavigationViewStyle())
            .tabItem {
                Image(systemName: (selected == 0 ? "house.fill" : "house"))
                Text("Home")
            }.tag(0)
            NavigationView {
                CategoryView(dm: DownloadManager())
            }
            .navigationViewStyle(DoubleColumnNavigationViewStyle())
            .tabItem {
                Image(systemName: (selected == 1 ? "text.justify" : "text.justify"))
                Text("Categorie")
            }.tag(1)
            NavigationView {
                GalleryView(dm: DownloadManager())
            }
            .navigationViewStyle(DoubleColumnNavigationViewStyle())
            .tabItem {
                Image(systemName: (selected == 2 ? "photo.fill" : "photo"))
                Text("Galleria")
            }.tag(2)
            NavigationView {
                SearchView()
            }
            .navigationViewStyle(DoubleColumnNavigationViewStyle())
            .tabItem {
                Image(systemName: (selected == 3 ? "magnifyingglass" : "magnifyingglass"))
                Text("Ricerca")
            }.tag(3)
            NavigationView {
                FavoritesView()
            }
            .navigationViewStyle(DoubleColumnNavigationViewStyle())
            .tabItem {
                Image(systemName: (selected == 4 ? "star.fill" : "star"))
                Text("Preferiti")
            }.tag(4)
        }
        .accentColor(.white)
        .onAppear() {
            UINavigationBar.appearance().isTranslucent = false
            UINavigationBar.appearance().barTintColor = UIColor(red: 112.0/255.0, green: 90.0/255.0, blue: 143.0/255.0, alpha: 1.0)
            UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            UITabBar.appearance().barTintColor = UIColor(red: 112.0/255.0, green: 90.0/255.0, blue: 143.0/255.0, alpha: 1.0)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Please help, I'm going crazy from trying! Thank you!

  • After working a bit on the code, I added the "UINavigationBar.appearance().backgroundColor" properties into the "onAppear" section, so now the part of the NavigationBar that was gray has become the color I wanted...except for the StatusBar area, that is still gray... Any suggestions? – pierofrezza Jan 11 '21 at 17:37
  • After searching a bit, I found the comment from the user ARR in this question [link](https://stackoverflow.com/questions/59394117/swiftui-navigation-bar-and-status-bar-make-them-same-color), and that is the one that has worked for me, so... problem solved! – pierofrezza Jan 13 '21 at 22:57
  • the barTintColor is only for inline Titles, for large Titles use the backgroundColor. – finalpets Aug 05 '21 at 19:33

0 Answers0