skip to Main Content

I can’t find a solution to change my background color view, I tried a lot of options and nothing works.

I’m trying solutions but the isn’t changing

There is my struct of the code:

struct ContentView: View {
var body: some View {        
            VStack {
                Text("Trying ColorView")
                    .font(.largeTitle)
                    .bold()
                Button("ColorView") {
                    
                }
            }      
        .accentColor(Color.black)
}

}

3

Answers


  1. You change a background color with the background modifier.

    If you want to change the background color of the whole view, you should create a ZStack that spans the whole view with a background modifier.

    ZStack { 
        ...
    }
    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
    .background(Color(.blue)
    
    Login or Signup to reply.
  2. First of all you have already mistakes in your posted code above, your XCode should normally tell you that.

    Which view you want to change..?
    This might be a solution… You can change it like you need it.

    struct testViewTwo: View {
    
        var body: some View {
            NavigationView {
                VStack {
                    VStack(spacing: -15) {
                    HStack {
                        HStack {
                           Text("Hello World")
                        }.background(Color.blue)
                    }.foregroundColor(Color.white)
                }
            }.background(Image("Background"))
            }
        }
    }
    

    enter image description here

    Login or Signup to reply.
  3. You can simply use Color("Green") to change the color. Here’s a sample.

    var body: some View {
        
        NavigationView{
            VStack {
                VStack(spacing: 15){
                     HStack {
                         Color("Green")
                           .edgesIgnoringSafeArea(.top)
                     }
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search