I'm unable to set color of entire background in SwiftUI forms or lists

31 views Asked by At

I need to control the background color of a form in SwiftUI particularly because this area appears white in Xcode simulators but black in devices. SimulatoriPhone

Admittedly I am new to SwiftUI, but I could not find an answer anywhere I searched. The closest I got was ".scrollContentBackground(.hidden)" but it did not touch the area between the yellow (form) and green (VStack). Any help appreciated.

struct ContentView: View {
    var body: some View {
        ZStack {
                VStack {
                    Text("Calculator").font(.system(size: 25))
                    Form{
                        Section("Intercept and Azimuth") {
                            VStack{
                                HStack{
                                    Text("Hc: ")
                                    Text("HCmin:")
                                }
                                HStack{
                                    Text("Z:    ")
                                }
                            }   //end VStack - result fields
                            .background(Color.green)
                        }  // end section
                    } //End form
                    .background(Color.yellow)
                    .frame(height:200)
                    .scrollContentBackground(.hidden)
                }//end vstack
                .padding()
            }//end zstack
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(.teal)
        }//end view
    }
1

There are 1 answers

0
Asad On

Default padding is applied to the contents of the section. To change the default padding, use listRowInsets(_:) modifier. Please try the following solution and workaround.

Form {
    Section("Intercept and Azimuth") {
        VStack(alignment: .leading) {
            HStack{
                Text("Hc: ")
                Text("HCmin:")
            }

            HStack{
                Text("Z:    ")
            }
        }   //end VStack - result fields
        .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
        .padding(10)
        .frame(maxWidth: .infinity, alignment: .leading)
        .background(Color.green)
    }  // end section
} //End form
.background(Color.yellow)
.frame(height:200)
.scrollContentBackground(.hidden)