In SwiftUI the TabView
changes the foreground color of the TabItem
when it is selected. But I need to keep the TabItem
foreground one color (selected or not) and changed the background color. Here is an example:
TabView with TabItem background color changes
It appears that SwiftUI's TabView
wants only the foreground color to change. I have tried using UITabBar.appearance()
, but I have found no way to change the TabItem
's background color dynamically. Here is an example:
struct ContentView: View {
var body: some View {
TabView {
FirstView()
.tabItem {
Label(title: {Text("First")}, icon: {Image(systemName: "1.lane")})
}
SecondView()
.tabItem{
Label(title: {Text("Second")}, icon: {Image(systemName: "2.lane")})
}
ThirdView()
.tabItem{
Label(title: {Text("Third")}, icon: {Image(systemName: "3.lane")})
}
FourthView()
.tabItem{
Label(title: {Text("Fourth")}, icon: {Image(systemName: "4.lane")})
}
}
.tint(Color.white)
.onAppear() {
let appearance = UITabBar.appearance()
appearance.isTranslucent = false
appearance.backgroundColor = UIColor(.gray)
appearance.tintColor = UIColor.white
appearance.unselectedItemTintColor = UIColor.white
}
}
Is this possible with SwiftUI's TabView or will I need to create a new fully custom TabView?