简体   繁体   中英

TabView with different TabBar backgroundColor on each Tab SwiftUi

I can't seem to get this simple problem working. I have a TabView with multiple Tabs, where depending on the selectedTab, the TabBar should have a different background. It should look like this, but this won't work, any help would be highly appreciated:

TabView(selection: $selectedTab){
   NavigationView{
      Home()
   }
   .navigationViewStyle(StackNavigationViewStyle())
   .tabItem{
            Image(systemName: "house")
   }.tag("home")
   .onAppear{
       UITabBar.appearance().backgroundColor = UIColor.white
   }
    

   NavigationView{
      Post()
   }
   .navigationViewStyle(StackNavigationViewStyle())
   .tabItem{
            Image(systemName: "plus")
   }.tag("plus")
   .onAppear{
       UITabBar.appearance().backgroundColor = UIColor.clear
   }
}

.background() method should do the trick. Would look something like this:

TabView(selection: $selectedTab){
    ZStack {
        Image(systemName: "house")
    }.tag("Home")
        .frame(width: 500, height: 500)
        .background(Color.red)
    
    ZStack {
        Image(systemName: "plus")
    }.tag("Plus")
        .frame(width: 500, height: 500)
        .background(Color.blue)
}.tabViewStyle(.page)

You can use any kind of container instead of the ZStack , or you could even apply the methods used on ZStack s directly to the images. Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM