0

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.

CodeSwift
  • 66
  • 1
  • 6
  • Does this answer your question https://stackoverflow.com/a/63179684/12299030? – Asperi Jul 26 '21 at 04:29
  • @Asperi Huh, it hadn't worked when I tried it earlier, but now it seems to be working perfectly fine. Thank you so much!! – CodeSwift Jul 26 '21 at 05:09
  • @Asperi Sorry for the late update, but I just noticed that your solution doesn't seem to work when I run it on actual device. When I tried it, only the first view was transparent, but the rest remained opaque. Are you aware of any workarounds that might work on iOS devices as well? Thank you. – CodeSwift Jul 28 '21 at 15:57
  • @CodeSwift do you have any luck with figuring out _makeView and _makeViewList? I'm trying to make a custom style for a list, and have no clue where to look... – nrudnyk Sep 20 '21 at 16:46
  • @nrudnyk Sorry, unfortunately I haven't been able to figure it out either :( Though I think you might have better luck trying to make a custom list view from scratch – CodeSwift Sep 23 '21 at 18:29
  • @CodeSwift yeah, that was my idea after facing the wall with custom style. But SwiftUI doesn't support custom preview for draggable item, so it's impossible to recreate similar to default list functionality. Still have to stick to UIKit here... – nrudnyk Oct 04 '21 at 14:38

0 Answers0