I am trying to change the color of selected tab in TabBar, but nothing worked. I can change the TabBar backgroundColor
by writing
struct ContentView: View {
init() {
UITabBar.appearance().backgroundColor = UIColor.purple
}
var body: some View {
}
}
In swift, we set tintColor
and it does change the color of selected tab. But what do i need to do for swiftUI?
Here is my code,
TabView(selection: $selection) {
AView()
.tabItem {
VStack {
Image(systemName: "bubble.left.and.bubble.right")
Text("A Tab")
}
}.tag(0)
BView()
.tabItem {
VStack {
Image(systemName: "house")
Text("B Tab")
}
}.tag(1)
CView()
.tabItem {
VStack {
Image(systemName: "circle.grid.3x3")
Text("C Tab")
}
}.tag(2)
}
Any help with this? Thanks in advance!!
To change the background color and default tab item colors, some extra work is required as demonstrated below. As shown in lines 2-4, we can use UITabBar. appearance(). backgroundColor to modify the color of the tab bar.
Use accentColor
: https://developer.apple.com/documentation/swiftui/tabview/3368073-accentcolor
TabView {
// fill this out with your tabbed content
}
.accentColor(.orange)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With