1

点击此代码中的其中一个时,我想更改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)
    }
4

2 回答 2

3

你可以像这样改变图像,

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-----
于 2021-01-07T06:34:55.423 回答
1

你可以为它使用状态

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)
}
于 2021-01-07T06:40:26.970 回答