HomeiOS Developmentios - Change Background Colour of chosen button merchandise swiftui in foreach

ios – Change Background Colour of chosen button merchandise swiftui in foreach


Do this method utilizing a @State var choice, and .background modifier
on the Button.

struct ContentView: View {
    let arr = ["item-1","item-2","item-3","item-4"]
    @State var choice = "" // <-- right here, regulate to subcategory kind
    
    var physique: some View {
        ScrollView(.horizontal) {
            HStack {
                ForEach(arr, id: .self) { subcategory in
                    Button {
                        print("chosen SubCategory",subcategory)
                        // vm.getProducts(subcat_id: subcategory.id)
                        choice = subcategory  // <-- right here
                    } label: {
                        //  SubCatView(subcategory: subcategory)
                        Textual content(subcategory)
                    }
                    // --- right here
                    .background((choice == subcategory ? Colour.yellow : Colour.clear))
                }
            }
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments