Here's a complete example:
import SwiftUI
import MapKit
struct Theme {
static let primary = Color.red
static let secondary = Color.green
static let background = Color.gray
}
@MainActor
struct ViewTabs: View {
@State var tag: UInt8 = 1
var body: some View {
TabView(selection: $tag) {
Map()
.tag(0)
.tabItem { Label("Map", systemImage: "map") }
Text("hello")
.tag(1)
.tabItem { Label("Text", systemImage: "character") }
Image(systemName: "dog")
.tag(2)
.tabItem { Label("Dog", systemImage: "dog") }
}
.onAppear(perform: ViewTabs.styleTabBar)
}
static func styleTabBar () {
let normalTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(Theme.secondary)]
UITabBarItem.appearance().setTitleTextAttributes(normalTextAttributes, for: .normal)
let selectedTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(Theme.primary)]
UITabBarItem.appearance().setTitleTextAttributes(normalTextAttributes, for: .selected)
let tabBarItemAppearance = UITabBarItemAppearance()
tabBarItemAppearance.normal.titleTextAttributes = normalTextAttributes
tabBarItemAppearance.normal.iconColor = UIColor(Theme.secondary)
tabBarItemAppearance.selected.titleTextAttributes = selectedTextAttributes
tabBarItemAppearance.selected.iconColor = UIColor(Theme.primary)
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.backgroundColor = UIColor(Theme.background)
tabBarAppearance.inlineLayoutAppearance = tabBarItemAppearance
tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
tabBarAppearance.compactInlineLayoutAppearance = tabBarItemAppearance
UITabBar.appearance().isTranslucent = false
UITabBar.appearance().standardAppearance = tabBarAppearance
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
}
}
#Preview {
ViewTabs()
}