Tuesday, 10 November 2020

SwiftUI - How to remove white background behind List that is styled with SidebarListStyle, in portrait mode

I have a list styled with SidebarListStyle (new in iOS 14) inside a NavigationView.

struct ContentView: View {
    let data = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen"]
    
    var body: some View {
        NavigationView {
            List {
                Section(header: Text("Section")) {
                    ForEach(data, id: \.self) { word in
                        Text(word)
                    }
                }
            }
            .listStyle(SidebarListStyle())
            .navigationBarTitle(Text("Title"), displayMode: .large)
        }
    }
}

The problem is that there is a white rectangle behind each of the rows in the list, but only in portrait mode. It's fine in landscape.

I don't want that white background, anyone know how to remove it? Also, when launching the app, it seems to glitch -- at first it's fine, then it adds the white background.

Weirdly, if I add .navigationViewStyle(StackNavigationViewStyle()) to the NavigationView, the white background disappears and it launches fine.

struct ContentView: View {
    let data = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen"]
    
    var body: some View {
        NavigationView {
            List {
                Section(header: Text("Section")) {
                    ForEach(data, id: \.self) { word in
                        Text(word)
                    }
                }
            }
            .listStyle(SidebarListStyle())
            .navigationBarTitle(Text("Title"), displayMode: .large)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

Left: How it launches, right: How it looks after launching

↑ This is how I want it to be.

However, now the landscape mode is limited to a full-width list, which I don't want either.

Edit: @Omid's answer

I added a background color to match the default one:

Text(word)
.listRowBackground(Color(UIColor.systemGroupedBackground))

But the launching glitch is still there.



from SwiftUI - How to remove white background behind List that is styled with SidebarListStyle, in portrait mode

No comments:

Post a Comment