[ACCEPTED]-iOS 16 SwiftUI List Background-ios16

Accepted answer
Score: 25

iOS 16

Now they use UICollectionView for backend, so an updated 2 workaround is to change corresponding background 1 colors:

demo

Main part:

extension UICollectionReusableView {
    override open var backgroundColor: UIColor? {
        get { .clear }
        set { }
    }
}

struct ContentView: View {
    init() {
        UICollectionView.appearance().backgroundColor = .clear
    }
//...

Test module on GitHub

Score: 15

For that purpose, I created a custom identifier 1 that hides this custom scroll background.

struct ListBackgroundModifier: ViewModifier {

    @ViewBuilder
    func body(content: Content) -> some View {
        if #available(iOS 16.0, *) {
            content
                .scrollContentBackground(.hidden)
        } else {
            content
        }
    }
}

Usage:

List {
    ...
}
.modifier(ListBackgroundModifier())
Score: 12

iOS 16 adds a new API, View.scrollContentBackground, to customize background 3 colors for scrollable Views including List. This 2 was added in beta 3 (release notes).

List {
    Text("One")
    Text("Two")
    Text("Three")
}
.scrollContentBackground(.red)

If you'd like to 1 make it clear, you can use Color.clear or .hidden like so:

.scrollContentBackground(.hidden)
Score: 1

As of Xcode 14.0.1 RC, the working code 1 is

.scrollContentBackground(.hidden)
.background(.purple)

More Related questions