简体   繁体   中英

How to change the background color for a Form in SwiftUI?

I wanna change that "light gray" background color for a form, but .foregroundColor(Color.blue) and .background(Color.blue) does not seem to work

struct ContentView : View {

 @State var value = ""

    var body: some View {
        Form {
            Section(header: Text("First Name")) {
                TextField($value)
            }
            Section(header: Text("Last Name")) {
                TextField($value)
            }
        }.foregroundColor(Color.blue)

    }
}

在此处输入图像描述

A Working Solution:

All SwiftUI's List s are backed by a UITableView in iOS. so you need to change the background color of the tableView . But since Color and UIColor values are slightly different, you can get rid of the UIColor .

struct ContentView: View {
    
    init(){
        UITableView.appearance().backgroundColor = .clear
    }
    
    @State var value = ""
    
    var body: some View {
        Form {
            Section(header: Text("First Name")) {
                TextField("First Name", text: $value)
            }
            Section(header: Text("Last Name")) {
                TextField("Last Name", text: $value)
            }
        }
        .foregroundColor(Color.blue)
        .background(Color.yellow)
    }
}

Now you can use Any background (including all Color s) you want

预习

Note that those top and bottom white areas are safe are and you can use .edgesIgnoringSafeArea() modifier to get rid of them.


Restore

Since UITableView.appearance().backgroundColor applies globally, you can use .onAppear modifier to change it in different views (since it is a global change). So you can use another onAppear or onDisappear to reset it back to what you want.

And the default colors are:

UIColor.systemGroupedBackground for the grouped style . And

UIColor.systemBackground for the plain style .

And they both have automatic support for both dark mode and light mode.

try this

.onAppear {
   UITableView.appearance().backgroundColor = .blue
}

The accepted answer by Mojtaba Hosseini, above, works but the init() statement is not a good place for the UITableView statement. This is because it "hard codes" the ContentView's init parameters. In this case it has none so everything works but if an @ObservedObject was added to the view then this would break the init function.

Much simpler just to add the UITable statement to the body , explicitly return the Form and delete the init() .

var body: some View {
    UITableView.appearance().backgroundColor = .clear
    return Form {...}
 }

However, setting the background colour on the Form usually works as expected. The fact that it is not working on the ContentView screen may be a bug.

Don't change the global appearance() .

You can use UITableView.appearance().backgroundColor = .red for example to set the Form 's background color to red. This can go in the init of the view, however this affects every List and Form .

Alternatively, you could use SwiftUI-Introspect to customise a single one by doing something like:

struct ContentView: View {
    @State private var value = ""

    var body: some View {
        Form {
            Section(header: Text("First Name")) {
                TextField("First", text: $value)
            }

            Section(header: Text("Last Name")) {
                TextField("Last", text: $value)
            }
        }
        .introspectTableView { $0.backgroundColor = .systemBlue }
        .foregroundColor(Color.blue)
    }
}

结果

You can also add the following to each section to make the sections blue too:

.listRowBackground(Color.blue)

As you already know, background isn't correct. It appears that Form may be similar to List . Try the colorMultiply modifier, which is what you use to deal with background color for List .

Two notes:

I copy/pasted your code into a beta 4 project and had two warnings - you probably should check it out.

Also, I'm expecting beta 5 within 24 hours. Hopefully it was beta 4 that was the "painful" upgrade (think Swift 2 >> Swift 3), but there's always a chance that beta 5 will contain changes that will break this! I'll try to remember to update my answer if so.

EDIT: If you want to know how I found this, I actually called up Apple's (very sketchy) documentation, found Form , and searched for "color". 17 hits, nearly all with nothing more than a placeholder. But I lurk on this tag (I'm still learning), tried accentColor (nope), then tried this (remembering this was the solution for Lists). Welcome to the bleeding edge!

I suspect that, like PresentationButton (betas 1 and 2) and PresentationLink (beta 3) and nowadays 'sheet (beta 4 means deprecating everything else), they will rename colorMultiply` (which is a totally meaningless term to this person coding since 1984) to something else soon. But it should work for today.

If you don't want to modify safe area of the Form , you can use ZStack as well:

struct ContentView: View {
    
    init(){
        
        UITableView.appearance().backgroundColor = .clear
    }
    
    @State var value = ""
    
    var body: some View {
        ZStack {
            Color(UIColor.systemYellow)
                .edgesIgnoringSafeArea(.all)
            Form {
                Section(header: Text("First Name")) {
                    TextField("First Name", text: $value)
                }
                Section(header: Text("Last Name")) {
                    TextField("Last Name", text: $value)
                }
            }
        }
    }
}

Copy these codes below each of your Form view:

Form {
    // Your form view 
} 
.onAppear { // ADD THESE AFTER YOUR FORM VIEW
    UITableView.appearance().backgroundColor = .clear 
}
.onDisappear { // CHANGE BACK TO SYSTEM's DEFAULT
    UITableView.appearance().backgroundColor = .systemGroupedBackground 
} 
.background(.yellow) // Add your background color

The solutions above didn't really work for what I was trying to achieve. I wanted an initial screen with a clear background for the form, and subsequent screens to have the default iOS systemGroupedBackground color. Using appear() and disappear() didn't work for me as switching between various tabs was leading to bugs in the appearance.

I came up with the following solution. It borrows from the solutions above.

For my ContentView screen, I inserted this code just inside the Struct.

init(){
    UITableView.appearance().backgroundColor = .clear
}

This is a global change that affects all forms.

For all forms where I wanted the default color to work, I inserted this code just outside the Form {}.

.background(Color(.systemGroupedBackground))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM