相关疑难解决方法(0)

SwiftUI 更改滚动视图内按钮的背景颜色

我正在尝试根据 isSelected 状态更改按钮的颜色但不起作用

struct Box: Identifiable  {
    var id: Int
    var title: String
    @State var isSelected: Bool
}

struct BoxView: View {
    var box: Box
    var body: some View{
        Button(action: {
            self.box.isSelected.toggle()
        }){
            Text(box.title)
                .foregroundColor(.white)
        }
    .frame(width: 130, height: 50)
        .background(self.box.isSelected ? Color.red : Color.blue)
    .cornerRadius(25)
    .shadow(radius: 10)
    .padding(10)

    }
}
Run Code Online (Sandbox Code Playgroud)

xcode scrollview ios swift swiftui

2
推荐指数
1
解决办法
2295
查看次数

标签 统计

ios ×1

scrollview ×1

swift ×1

swiftui ×1

xcode ×1