Reputation: 901
I want to change tabItem
icon image when tapped on one of the tabItem
in this code. How can it be done?
var body: some View {
ZStack {
TabView(selection:$selection) {
OffersView()
.tabItem ({
Image(systemName: "bag").renderingMode(.template)
Text("Offers")
})
.tag(1)
StepView()
.tabItem ({
Image(systemName: "figure.walk.circle").renderingMode(.template)
Text("Steps")
})
.tag(2)
ProfileView()
.tabItem {
Image(systemName: "person")
Text("Profile")
}
.tag(3)
}
.accentColor(Color("ColorOnboarding"))
.navigationBarBackButtonHidden(true)
}
Upvotes: 1
Views: 571
Reputation: 149
You can use state for it
var body: some View {
@State var isTapped = false
ZStack {
TabView(selection:$isTapped) {
OffersView()
.tabItem ({
Image(systemName: selection == 1 ? "bag" : "bag2").renderingMode(.template)
Text("Offers")
})
.tag(1)
StepView()
.tabItem ({
Image(systemName: selection == 2 ? "figure.walk.circle" : "figure.walk.circle2").renderingMode(.template)
Text("Steps")
})
.tag(2)
ProfileView()
.tabItem {
Image(systemName: "person")
Text("Profile")
}
.tag(3)
}
.accentColor(Color("ColorOnboarding"))
.navigationBarBackButtonHidden(true)
}
Upvotes: 1
Reputation: 18914
You can change image like this,
var body: some View {
ZStack {
TabView(selection:$selection) {
OffersView()
.tabItem ({
Image(systemName: selection == 1 ? "bag" : "bag2").renderingMode(.template)
Text("Offers")
})
.tag(1)
StepView()
.tabItem ({
Image(systemName: selection == 2 ? "figure.walk.circle" : "figure.walk.circle2").renderingMode(.template)
Text("Steps")
})
.tag(2)
// -----Other Code-----
Upvotes: 3