Skip to content

Instantly share code, notes, and snippets.

@dominicfreeston
Last active August 9, 2023 22:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominicfreeston/b026c056e27acc987cd4c37020583b58 to your computer and use it in GitHub Desktop.
Save dominicfreeston/b026c056e27acc987cd4c37020583b58 to your computer and use it in GitHub Desktop.
SwiftUI Transparent List Wrapper - Works in iOS 14
/// Wraps its content in a HostingViewController that has the backgroundColor of UITableView appearance
/// within it set to .clear, thus working around the issue where SwiftUI Lists always have white backgrounds,
/// without inadvertently affecting the appearance of all UITableViews in the app.
/// Setting the listRowBackground explicitly is still required, setting that to clear won't work.
struct ListAppearanceContainer<Content>: View, UIViewControllerRepresentable where Content: View {
let content: Content
class AppearanceContainerViewController<Content>: UIHostingController<Content> where Content: View {}
init(_ content: () -> Content) {
self.content = content()
}
func makeUIViewController(context: Context) -> AppearanceContainerViewController<Content> {
let tableView = UITableView.appearance(whenContainedInInstancesOf: [AppearanceContainerViewController<Content>.self])
tableView.backgroundColor = .clear
let vc = AppearanceContainerViewController(rootView: content)
vc.view.backgroundColor = .clear
return vc
}
func updateUIViewController(_ uiViewController: AppearanceContainerViewController<Content>, context: Context) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment