I've noticed that while most views in SwiftUI are transparent, TabView
seems to add an opaque background to each view. So my question is how might I be able to remove the said opaque background to make the view transparent?
I tried using this helper extension from this StackOverflow answer, only it seemed to make my first tab transparent and not the rest, and the compiler threw a lot of warnings and an occasional error, so I’m uncertain if that’s a safe way to remove the background.
I also bumped into another discussion on Hacking with Swift, which brought to light that some TabView
styles, namely PageTabViewStyle
, actually allowed for a transparent background. So I decided to try making a custom TabViewStyle
, only I'm completely stumped at this:
// Custom TabViewStyle to make view transparent
struct TransparentTabViewStyle: TabViewStyle {
static func _makeView<SelectionValue>(value: _GraphValue<_TabViewValue<TransparentTabViewStyle, SelectionValue>>, inputs: _ViewInputs) -> _ViewOutputs where SelectionValue : Hashable {
// code
}
static func _makeViewList<SelectionValue>(value: _GraphValue<_TabViewValue<TransparentTabViewStyle, SelectionValue>>, inputs: _ViewListInputs) -> _ViewListOutputs where SelectionValue : Hashable {
// code
}
}
I'm not sure what _GraphValue
, _TabViewValue
, and the other data types are but I assume they're some sort of hidden values for building the view?
I've also considered adding an extension to DefaultTabViewStyle
to modify the background there (if that's even possible), only I don't know where to access the TabView
's background since the only thing available seems to be self
.
My last resort would probably be making a custom tab bar from scratch, but I'd prefer not to. So what might be the optimal solution to making my TabView
's background transparent? And if it's making a custom a TabViewStyle
, could someone at least point me in the right direction for implementing the _makeView
and _makeViewList
functions?
Any help would be greatly appreciated!
Edit: For some reason it hadn't worked when I tried it before, but this answer seems to work: https://stackoverflow.com/a/63179684/12299030
Edit: Actually, this solution (https://stackoverflow.com/a/63179684/12299030) only seems to work on the Xcode simulators. I just tested my app on an actual device and the views were no longer transparent.