I want to change foreground and background colors of selected buttons but when I click on one button it select all buttons and change their colors too. I've found solution with ID but it works with custom button ios - SwiftUI - How to change the colour of all other buttons when pressing on button? - Stack Overflow Can I change colors without creating custom button?
@State var isSelected: Bool = false
HStack {
ForEach(category.terms, id: \.self) { attribute in
Button {
self.isSelected.toggle()
print("Attr pressed")
} label: {
Text(attribute.name)
.padding()
.foregroundColor(self.isSelected ?Color.white : Color.textFieldGrayColor)
}
.background(self.isSelected ? Color.blue : Color.grayButton)
.cornerRadius(10)
}
}