Last active
August 9, 2023 22:41
-
-
Save dominicfreeston/b026c056e27acc987cd4c37020583b58 to your computer and use it in GitHub Desktop.
SwiftUI Transparent List Wrapper - Works in iOS 14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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