Clear Background for Form Sections in Swiftui

Clear background for form sections in SwiftUI?

Even in SwiftUI 2 Form is built on top of UIKit, specifically UITableView.

You need to remove the default UITableViewCell's background (only once, preferably in the App init):

UITableViewCell.appearance().backgroundColor = UIColor.clear

and change the background using:

Section {
VStack {
Button("Button 1") {}
Spacer()
Button("Button 2") {}
}
}
.listRowBackground(Color.clear)

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


iOS 16

You can hide the default background to show the underling view by appealing the following modifier to the Form:

.scrollContentBackground(.hidden)


iOS 13 and 15

All SwiftUI's Lists are backed by a UITableViewin 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)
}
}
/*.scrollContentBackground(.hidden)*/ // this line will work only on iOS 16 and above
.foregroundColor(Color.blue)
.background(Color.yellow)
}
}

Now you can use Any background (including all Colors) you want

Preview

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.

Remove background of a cell in Forms - SwiftUI

I figured it out, all I had to do was add at the end of a stack the following:

.listRowBackground(Color(UIColor.systemGroupedBackground))

How can I remove background Color from section in SwiftUI?

Here is possible solution - use list row background color same to parent background.

Tested with Xcode 12.1 / iOS 14.1

demo

struct FormView: View {
var body: some View {
Form {
Section {
Button(action: {

}) {
HStack {
Spacer()
Text("Save")
.font(.headline)
.foregroundColor(.white)
Spacer()
}
}
.listRowInsets(.init())
.listRowBackground(Color.red) // << here !!
.frame(height: 50)
.background(Color.blue)
.cornerRadius(15)
}
}.background(Color.red).edgesIgnoringSafeArea(.all)
}
}

When dealing with a non-solid color, add

            .onAppear {
UITableViewCell.appearance().backgroundColor = UIColor.clear
}

and set

.listRowBackground(Color.clear)

SwiftUI 2 clear background for list section header

Solution found! The trick is to use a LazyVStack with pinned sections:

struct ContentView: View {
var body: some View {
ScrollView {
LazyVStack(pinnedViews:[.sectionHeaders]) {
Section(header: Text("List Header")) {
Text("Hi")
Text("Hi")
Text("Hi")
}
}
}
}
}

Fill the Section background in SwiftUI Form

Maybe this will help, at least in Simulator this seems to be a solution to your request:

        NavigationView {
Form {
Section {
HStack {
Text("Error message!")
Spacer()
}
.padding(.vertical)
.listRowInsets(EdgeInsets())
}
.padding(.horizontal)
.background(Color.red.opacity(0.2))

Section {
Text("Label 1")
}
Section {
Text("Label 2")
}
}
.navigationBarTitle("title", displayMode: .inline)
}

Remove/change section header background color in SwiftUI List

In beta 4, relativeWidth was deprecated. Code updated to reflect that.

Unfortunately, there's no quick parameter to set the background color. However, you can still do it:

Sample Image

import SwiftUI

struct ContentView : View {
var body: some View {
List {
ForEach(0...3) { section in
Section(header:
CustomHeader(
name: "Section Name",
color: Color.yellow
)
) {
ForEach(0...3) { row in
Text("Row")
}
}
}
}
}
}

struct CustomHeader: View {
let name: String
let color: Color

var body: some View {
VStack {
Spacer()
HStack {
Text(name)
Spacer()
}
Spacer()
}
.padding(0).background(FillAll(color: color))
}
}

struct FillAll: View {
let color: Color

var body: some View {
GeometryReader { proxy in
self.color.frame(width: proxy.size.width * 1.3).fixedSize()
}
}
}


Related Topics



Leave a reply



Submit