UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Bookworm - trying to add colors

Forums > 100 Days of SwiftUI

I'm trying to add colors to this NavigationStack. I want a single color for the entire background. At firt I got nothing. then I added a ZStack, even a VStack, but I get a red block between the top nav bar and the list. I know I'm overlooking something simple, but I can't quite figure it out.

There was an earlier post on the subject, but I cannot seem to apply the concepts on my view.

I appreciate your help.

var body: some View {

    NavigationStack(path: $path){
        ZStack {
            VStack {
                Color.red
                List(restaurants) {restaurant in
                    NavigationLink(value: restaurant) {
                        HStack {
                            Text(restaurant.location)
                            Text(restaurant.mainGrade)
                        }
                        .font(.system(size:18))
                        .foregroundColor(.green)
                    }
                }
                .navigationTitle("Restaurants")
                .toolbar {
                    Button("Add", systemImage: "plus") {
                        let restaurant = Restaurant(location: "New", eGrade: "A", sGrade: "A", cGrade: "A", rGrade: "A", bGrade: "A", mainGrade: "A")
                        modelContext.insert(restaurant)
                        path = [restaurant]
                    }
                    .navigationDestination(for: Restaurant.self) { restaurant in
                        EditRestaurauntView(restaurant: restaurant)
                    }
                }
            }
        }
    }

} }

2      

The same approach applies to your case. List is a scrollable content so you have to disable its background color first, then you can apply your own. No need for ZStack etc...

https://www.hackingwithswift.com/forums/100-days-of-swiftui/how-can-i-change-backgroundcolor-while-using-navigationstack/25159/25176

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.