33

Does anyone know how to change the background colour of a tabbed view bottom bar?

I have set the accent colour which changed the colour of my icons when I select each tab bar item.

I have tried setting the background to a colour but it doesn't change the back, and tried setting background to an image just to be sure but that also doesn't do anything.

Wondering if I need to specifically access the bottom bar somehow and then set a property on that?

AngryDuck
  • 4,358
  • 13
  • 57
  • 91

10 Answers10

37

SwiftUI 1.0 - Using named colors

Combining barTintColor and isTranslucent

For some reason I wasn't getting the full color of my named color when I used just barTintColor or even backgroundColor. I had to include isTranslucent too.

Here is my named color:

Named Color

Setting Just barTintColor

Just Bar Tint Color

(As you can see, it is slightly faded)

Setting Just backgroundColor

Just Background Color

(This darkens the bar a little bit)

Setting barTintColor & isTranslucent to False

isTranslucent & barTintColor

This combination is what did it for me:

UITabBar.appearance().isTranslucent = false
UITabBar.appearance().barTintColor = UIColor(named: "Secondary")
Community
  • 1
  • 1
Mark Moeykens
  • 15,915
  • 6
  • 63
  • 62
  • 3
    This worked for me and the accepted answer did not; I had tried to make the background white, and with the accepted answer it stayed gray (albeit slightly lighter than before). Thanks! – blub Jun 01 '20 at 20:14
  • I had to use: `UITabBar.appearance().scrollEdgeAppearance = appeareance` – Sajjon Feb 28 '22 at 06:50
35

Here is a solution. You can change appearance of the UITabBar and change the TabBar.

struct TabView: View {
    init() {
        UITabBar.appearance().backgroundColor = UIColor.blue
    }
    var body: some View {

        return TabbedView {
            Text("This is tab 1").tag(0).tabItemLabel(Text("tab1"))
            Text("This is tab 2").tag(1).tabItemLabel(Text("tab2"))
            Text("This is tab 3").tag(2).tabItemLabel(Text("tab3"))
        }
    }
}
Rubaiyat Jahan Mumu
  • 3,887
  • 1
  • 33
  • 35
  • 6
    Just a warning that this answer relies on SwiftUI to use `UITabBar` to represent a `TabbedView`. This could change at any time—for example, if Apple creates a pure swift tab bar. – mbxDev Jul 12 '19 at 17:13
  • 4
    In that case, I will update the answer or someone will. – Rubaiyat Jahan Mumu Jul 13 '19 at 05:34
  • Hi @mumu, I tried to do this but it somehow makes the tab color opaque. Do you have any suggestion for a workaround? – ygee Sep 24 '19 at 09:08
  • 2
    @grey - See the answer below. UITabBar.appearance().barTintColor = UIColor.blue – Yarm Oct 05 '19 at 16:33
18

In the init() add UITabBar.appearance().barTintColor = UIColor.blue

struct ContentView: View {

    @State private var selection = 1

    init() {
        UITabBar.appearance().barTintColor = UIColor.blue
    }

    var body: some View {
        TabView (selection:$selection){
            Text("The First Tab")
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text("First")
            }
            .tag(1)
            Text("Another Tab")
                .tabItem {
                    Image(systemName: "2.square.fill")
                    Text("Second")
            }.tag(2)
            Text("The Last Tab")
                .tabItem {
                    Image(systemName: "3.square.fill")
                    Text("Third")
            }.tag(3)
        }
        .accentColor(.white)
    }
}

enter image description here

stackich
  • 3,607
  • 3
  • 17
  • 41
Yarm
  • 1,178
  • 4
  • 16
  • 29
6

Its Work for me in latest Versions

var body: some View {
   
    TabView{
        
            Text("Zain ahmed")
            .font(.system(size: 30, weight: .bold, design: .rounded))
            .tabItem {
                Label("Home", systemImage: "house.fill")
            }
        
        Text("Bookmark Tab")
            .font(.system(size: 30, weight: .bold, design: .rounded))
            .tabItem {
                Label("Bookmark", systemImage: "bookmark.circle.fill")
            }
     
   
            
        
    }
    .onAppear() {
        UITabBar.appearance().backgroundColor = .lightGray
    }
   

}
Zain Ahmed
  • 179
  • 2
  • 3
3

This one looks like a working solution based on the latest version of Swift and SwiftUI

struct TabBar: View {
    init() {
        UITabBar.appearance().barTintColor = UIcolor.black

    var body: some View {
        TabView {
            HomeView().tabItem {
                Image(systemName: "house.fill")
                Text("Home")
            }
            MapView().tabItem {
                Image(systemName: "mappin.circle.fill")
                Text("Map")
            }
        }
        .edgesIgnoringSafeArea(.top)
    }
}

where HomeView() and MapView() are just some other views created earlier that will be displayed on tap.

Roman Shelkford
  • 854
  • 6
  • 4
3

It is important to set the colors for UITabBar before the TabView is shown. If not using a custom view with initializer, then you must make sure it is called before the TabView is loaded, for instance in the AppDelegate (when using the "UIKit App Delegate" in the project life cycle or otherwise adding it for "SwiftUI App" life cycle).

Then you can configure it with a UITabBarAppearance() object, for instance like so:

let tabBarAppeareance = UITabBarAppearance()
tabBarAppeareance.shadowColor = .gray // For line separator of the tab bar
tabBarAppeareance.backgroundColor = .black // For background color
UITabBar.appearance().standardAppearance = tabBarAppeareance
atineoSE
  • 3,597
  • 4
  • 27
  • 31
3

In case if you need change background of unselected item and top line as well then you can stuck. Next is what will work for me. We will start from this one: origin In first iteration I change everything except top line:

struct ContentView: View {
    @ObservedObject var model: ContentViewModel

    init(model: ContentViewModel) { 
        self.model = model

        UITabBar.appearance().isTranslucent = false
        UITabBar.appearance().unselectedItemTintColor = UIColor(Color.primary)
        UITabBar.appearance().barTintColor = UIColor(Color("tab_background"))
    }

    var body: some View {
        NavigationView {
            TabView(selection: $model.selectedTab) {...}
        }
    }
}

result-1 But after that, I realize that I can't change the color of this line in the same way. So I will use @atineoSE answer. But realize that set the UITabBar.appearance().standardAppearance will totally override my previous customization. So I need to change it - here is the final code and result:

 init(model: ContentViewModel) {
    self.model = model

    let itemAppearance = UITabBarItemAppearance()
    itemAppearance.normal.iconColor = UIColor(Color.primary)

    let appeareance = UITabBarAppearance()
    appeareance.shadowColor = UIColor(Color("tab_separator"))
    appeareance.backgroundColor = UIColor(Color("tab_background"))
    appeareance.stackedLayoutAppearance = itemAppearance
    appeareance.inlineLayoutAppearance = itemAppearance
    appeareance.compactInlineLayoutAppearance = itemAppearance

    UITabBar.appearance().standardAppearance = appeareance
}

result

WINSergey
  • 1,977
  • 27
  • 39
2

TabbedView has been deprecated, for now you can try:

struct AppTabbedView: View {

    @State private var selection = 3

    init() {
        UITabBar.appearance().backgroundColor = UIColor.blue
    }
    var body: some View {
        TabView (selection:$selection){
            Text("The First Tab")
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text("First")
            }
            .tag(1)
            Text("Another Tab")
                .tabItem {
                    Image(systemName: "2.square.fill")
                    Text("Second")
            }.tag(2)
            Text("The Last Tab")
                .tabItem {
                    Image(systemName: "3.square.fill")
                    Text("Third")
            }.tag(3)
        }
        .font(.headline)
    }
}
Amir.n3t
  • 2,859
  • 3
  • 21
  • 28
1
 init() {
    UITabBar.appearance().barTintColor = .white
    UITabBar.appearance().backgroundColor = .white
    
   }
-1

While this is great for light mode, when you switch to dark mode, the background for the tabbar stays the color you have selected. Any way to make the bar go to black when dark mode is sl

James
  • 15
  • 6