1

I have the following:

var body: some View {
        NavigationView {
            VStack {
                 Text("Hello")
            }.navigationBarTitle("Edit Profile", displayMode: .inline)
             .background(NavigationConfiguration { nc in
                 nc.navigationBar.barTintColor = .red
                 nc.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.black]
            })
        }.navigationViewStyle(StackNavigationViewStyle())
}

For the configuration of the nav bar i have:

struct NavigationConfiguration: UIViewControllerRepresentable {
    var configuration: (UINavigationController) -> Void = { _ in }
    func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationConfiguration>) -> UIViewController {
        UIViewController()
    }
    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationConfiguration>) {
        if let nc = uiViewController.navigationController {
            self.configuration(nc)
        }
    }
}

But for some reason the top status bar is being left out and the colour is not being applied to it:

Status bar image

How can the red colour also be applied to the status bar above the nav bar, I want it to be the same colour.

I've tried the below, but this fails:

init() {
    UINavigationBar.appearance().backgroundColor = .red
}
Gurmukh Singh
  • 1,875
  • 3
  • 24
  • 62

2 Answers2

6

Try the following (it should work, at least as tested with Xcode 11.4 / iOS 13.4, but someone reported it was not, so it might be dependent)

demo

init() {
       let navBarAppearance = UINavigationBarAppearance()
       navBarAppearance.configureWithOpaqueBackground()
       navBarAppearance.backgroundColor = UIColor.systemRed

       UINavigationBar.appearance().standardAppearance = navBarAppearance
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
3

EDIT 1: Checkout my new answer: https://stackoverflow.com/a/62031788/8023700 It has detailed description and screenshot as well.

Try using this modifier as this gives you freedom to change color based on each view.

struct NavigationBarModifier: ViewModifier {

var backgroundColor: UIColor = .clear

init(backgroundColor: UIColor, tintColor: UIColor = .white) {
    self.backgroundColor = backgroundColor
    
    let coloredAppearance = UINavigationBarAppearance()
    coloredAppearance.backgroundColor = backgroundColor
    coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
    
    UINavigationBar.appearance().standardAppearance = coloredAppearance
    UINavigationBar.appearance().compactAppearance = coloredAppearance
    UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
    UINavigationBar.appearance().tintColor = tintColor
}

func body(content: Content) -> some View {
    ZStack{
        content
        VStack {
            GeometryReader { geometry in
                Color(self.backgroundColor)
                    .frame(height: geometry.safeAreaInsets.top)
                    .edgesIgnoringSafeArea(.top)
                Spacer()
            }
        }
    }
}}

I used it on below View:

struct DummyNavigationView: View {
var body: some View {
    NavigationView {
        VStack {
            NavigationLink(destination: Text("Page 2")) {
                Text("Go to detail")
            }
        }
        .navigationBarTitle("Edit Profile", displayMode: .inline)
    }
    .modifier(NavigationBarModifier(backgroundColor: .red, tintColor: .black))
}}
user832
  • 796
  • 5
  • 18