ScrollView导致SwiftUI中的错误按钮

我一直在尝试在SwiftUI中创建表单,但是由于使用“ Form()”的局限性,我改为选择使用包含按钮的ForEach循环创建ScrollView。当我运行该项目并尝试单击按钮时,它将单击不正确的按钮,除非我滚动视图。我是SwiftUI的新手,但无法弄清楚。

我尝试制作不同尺寸的ScrollView,但似乎没有关联

struct DropDown: View {

var datos: [String]
var categoria: String

@State var titulo: String = "Seleccione"
@State var expand = false

var body: some View {

    VStack {
        Text(categoria).fontWeight(.heavy).foregroundColor(.white)
        HStack {
            Text(titulo).fontWeight(.light).foregroundColor(.white)
            Image(systemName: expand ? "chevron.up" : "chevron.down").resizable().frame(width: 10,height: 6).foregroundColor(.white)

        }.onTapGesture {
            self.expand.toggle()
        }

        if expand {
            ScrollView(showsIndicators: true) {
                ForEach(0..<self.datos.count){ nombre in
                    Button(action: {
                        print(self.datos[nombre])
                        self.titulo = self.datos[nombre]
                        self.expand.toggle()
                        diccionarioDatos[self.categoria] = self.titulo
                        print(diccionarioDatos)
                    }) {
                        Text(self.datos[nombre]).foregroundColor(.white)
                    }
                }
            }
            .frame(maxHeight: 150)
            .fixedSize()
        }
    }
    .padding()
    .background(LinearGradient(gradient: .init(colors: [.blue,.green]),startPoint: .top,endPoint: .bottom))
    .cornerRadius(20)
    .animation(.spring())
}

}

I clicked on "2018" under "Modelo" and "2015" got selected for some reason

This is how the dropdown menu looks like

jing81024 回答:ScrollView导致SwiftUI中的错误按钮

请考虑使用.onTapGesture代替action作为按钮。

Button(action: {},label: {
    Text(self.datos[nombre]).foregroundColor(.white)
}).onTapGesture{
    print(self.datos[nombre])
    self.titulo = self.datos[nombre]
    self.expand.toggle()
    diccionarioDatos[self.categoria] = self.titulo
    print(diccionarioDatos)
}
,

当我测试时,观察到的行为归因于可动画属性的顺序。在您的情况下,将圆角移入背景本身即可解决问题。

所以不是

library(dplyr)
library(purrr)

facs <- c("Easy","Match","Hard")

bind_cols(df,set_names(map_dfc(facs,~ rowSums(df == .,na.rm = T)),facs))

#### OUTPUT ####

  userID Score Task_Alpha Task_Beta Task_Charlie Task_Delta Easy Match Hard
1   3108 -8.00       Easy      Easy         Easy       Easy    4     0    0
2   3207  3.00       Hard      Easy        Match      Match    1     2    1
3   3350  5.78       Hard      Easy         Hard       Hard    1     0    3
4   3961 10.00       Easy      <NA>         Hard       Hard    1     0    2
5   4021 10.00       Easy      Easy         <NA>       Hard    2     0    1

使用

.background(
    LinearGradient(gradient: .init(colors: [.blue,.green]),startPoint: .top,endPoint: .bottom)
)
.cornerRadius(20)
本文链接:https://www.f2er.com/3148661.html

大家都在问