swiftswiftuinavigationbar

Navigation bar SwiftUI toolbar background


I set up my nav bar like this:

        .navigationTitle(selectedTab == .users ? "Working with GET request" : "Working with POST request")
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(UIConstraints.primary, for: .navigationBar)
        .toolbarBackground(.visible, for: .navigationBar)

get this: enter image description here

but I don't want yellow color to go all the way to the top where wifi and battery icons will be, I want it to stop before the line on the top

I am struggling to formulate a question about this and find any info, please advise


Solution

  • AFAIK, .toolbarBackground(for: .navigationBar) always overwhelms statusBar too. So you may want to try this approach by setting UINavigationBar.appearance():

    struct ContentView: View {
        init() {
            UINavigationBar.appearance().backgroundColor = UIColor.yellow
        }
       
        var body: some View {
            ...
            //.toolbarBackground(UIConstraints.primary, for: .navigationBar)
            //.toolbarBackground(.visible, for: .navigationBar)
        }
    }
    

    Output:

    enter image description here