I want to use the default button style, and I know we can set the tint
to change the background color of a button. But how can I change the color when user hover the button, in macOS or tvOS? The hover button color is still a white with a transparency even it shows other color when not in hover.
SwiftUI: Change the button color in hover
Use .onHover.
Here is an example (applied to TextField, but easy to adapt to Button).
struct ContentView: View {
@State var isHover = [false, false]
@State var text = "OUT"
@State var textlist = ["OUT", "OUT"]
var body: some View {
// https://developer.apple.com/forums/thread/725575
VStack {
ForEach(0 ..< textlist.count, id: \.self){ index in
Text(textlist[index])
.frame(width: 80, height: 40)
.foregroundColor(self.isHover[index] ? .yellow : .blue)
.onHover(perform: { flag in
self.isHover[index] = flag
if flag {
textlist[index] = "IN"
} else {
textlist[index] = "OUT"
}
})
}
}
.frame(width: 200, height: 200)
}
}