Search code examples
swiftuitabbar

How to make SwiftUI TabBar background always transparent


I am trying to completely remove the background of a TabView in SwiftUI, but I can't seem to find a solution.

I've implemented my own background to the TabView like this:

TabView {
    ZStack {
        DriveView()
        BackgroundTabBar()
    }.tabItem {
        Image("tab_drive")
        Text("Drive")
    }
}

And then:

init() {
    UITabBar.appearance().unselectedItemTintColor = .white
    UITabBar.appearance().backgroundColor = .clear
}

But, when I implement a ScrollView I get the default opaque background also: Tab Bar problem

How do I get completely rid of the background in the TabBar, also when scrolling?


Solution

  • I managed to find another solution where I was still able to control the appearance of the tab items using this:

    init() {
        let appearance = UITabBarAppearance()
        appearance.configureWithTransparentBackground() // <- HERE
        appearance.stackedLayoutAppearance.normal.iconColor = .white
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    
        appearance.stackedLayoutAppearance.selected.iconColor = UIColor(Color.accentColor)
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(Color.accentColor)]
    
        UITabBar.appearance().standardAppearance = appearance
    }