I have Row View:
struct TestRow: View {
var rowText: String
var body: some View {
HStack {
Text(rowText)
Spacer()
Image(systemName: "heart.circle.fill")
}
.listRowBackground(Color.red)
}
.listRowbackground work correctly, but if I add contextMenu, background changing to default (white) and don't changing from contextMenu :
struct TestRow: View {
@State var rowColor: Color = Color.mint
var rowText: String
var body: some View {
HStack {
Text(rowText)
Spacer()
Image(systemName: "heart.circle.fill")
}
// .background(rowColor) -> this code working with contextMenu
.listRowBackground(rowColor)
.contextMenu {
VStack {
Button {
//action code
rowColor = Color.red
} label: {
MenuButton(colorOrb: "red_circle", buttonText: "High")
}
Button {
//action code
rowColor = .yellow
} label: {
MenuButton(colorOrb: "yellow_circle", buttonText: "Medium")
}
Button {
//action code
rowColor = .green
} label: {
MenuButton(colorOrb: "green_circle", buttonText: "Normal")
}
}
}
}
}
If I use .background(rowColor) instead .listRowBackground(rowColor) code work correctly, but I need colorise all row, not HStack only.
struct Item: Identifiable {
var id: Int
var color: Color
}
struct ContentView2: View {
@State var items: [Item] = [Item(id: 1, color: .red),
Item(id: 2, color: .yellow),
Item(id: 3, color: .blue)]
var body: some View {
VStack {
List {
ForEach($items) { $item in
TestRow(rowColor: $item.color, rowText: "\(item.id)")
.listRowBackground(item.color)
}
}
}
}
}
struct ContentView2_Previews: PreviewProvider {
static var previews: some View {
ContentView2()
}
}
struct TestRow: View {
@Binding var rowColor: Color
let rowText: String
var body: some View {
HStack {
Text(rowText)
Spacer()
Image(systemName: "heart.circle.fill")
}
// .background(rowColor) -> this code working with contextMenu
.contextMenu {
Button {
//action code
rowColor = Color.red
} label: {
Text("red")
}
Button {
//action code
rowColor = .yellow
} label: {
Text("yellow")
}
Button {
//action code
rowColor = .green
} label: {
Text("green")
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.