ial*_*fan 2 xcode scrollview ios swift 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)
试试这个方法。
struct Box: Identifiable {
var id: Int
var title: String
}
struct BoxView: View {
var box: Box
@State var selectedBtn: Int = 1
var body: some View {
ForEach((1...10).reversed(), id: \.self) { item in
Button(action: {
self.selectedBtn = item
}){
Text(self.box.title)
.foregroundColor(.white)
}
.frame(width: 130, height: 50)
.background(self.selectedBtn == item ? Color.red : Color.blue)
.cornerRadius(25)
.shadow(radius: 10)
.padding(10)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2295 次 |
| 最近记录: |