更改标签栏图标图像 SwiftUI

Change Tabbar Icon Image SwiftUI

我想在点击此代码中的 tabItem 之一时更改 tabItem 图标图像。怎么做到的?

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)
    }

你可以这样改变图片,

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-----

你可以为它使用状态

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)
}