How to make SwiftUI List translucent? (macOS!)

SwiftUI List comes with embedded scroll functionalty, which is nice, except...


that view has a (white) background at a higher lever than any modifyable view. Result: a background view is layered behind the white background of the scroll view. In the debugger I see that the List encapsulates a Hosting View>View Host>ListCoreScrollView. The latter shows opaque. Thusly, trying to change the background of the List has no effect: the background view will be behind the white scroll view.


You can easily try it yourself. Create a list with some random rows.

List { ForEach(rows.items)


Run the app, then look at the View Debugger: rotate the view and you will see the auto generated while layer in the view hierarchy.


My first attempt was setting the background color (to no avail, as described above). I also tried adding a custom ViewModifier. Inside a viewModifier one can set the UITableView.appearance(). However, that is IOS only, it does not work on macOS. Using VisualEffectBackground as described here did not get me anywhere either...


The only thing that did yield some result is changing the listStyle. Adding .listStyle(SidebarListStyle()) does result in the background that I like, but has another, even worse, side effect: dragging does not work as needed. (I tried modifying that behavior as well, but that causes a whole range of new issues, so gave up).


In the end I have spend many hours trying to change something simple as the (background) color of a List. I want to implement a sidebar that is more like the Finders sidebar (which is not white either).


Just repeating to avoid irrelevant answers: macOS platform, not IOS.

Answered by HansKe in 641214022
Fixed in Big Sur.

This is iOS, not MacOS, for sure, but there are information that may help you find a solution.


In additiuon, I guess this applies to OSX as well:

The very common issue is we can not remove the background color of SwiftUI's HostingViewController (yet), so we can't see some of the views like navigationView through the views hierarchy. You should wait for the API or try to fake those views (not recommended).


Accepted Answer
Fixed in Big Sur.
How to make SwiftUI List translucent? (macOS!)
 
 
Q