ScrollView вызывает глючные кнопки в SwiftUI - PullRequest
1 голос
/ 07 ноября 2019

Я пытался создать форму в SwiftUI, но из-за ограничений, использующих «Form ()», я вместо этого решил создать ScrollView с циклом ForEach, который содержит кнопку. Когда я запускаю проект и пытаюсь нажимать на кнопки, он нажимает неправильную, если я не прокручиваю вид. Я новичок в 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())
}

}

Я нажал на «2018» под «Modelo», и почему-то выбрали «2015»

Вот так выглядит выпадающее меню

Ответы [ 2 ]

0 голосов
/ 07 ноября 2019

Как я проверял, наблюдаемое поведение обусловлено порядком анимируемых свойств. В вашем случае перемещение углов закругления в фон само по себе решает проблему.

Поэтому вместо

.background(
    LinearGradient(gradient: .init(colors: [.blue, .green]), startPoint: .top, endPoint: .bottom)
)
.cornerRadius(20)

используйте

.background(
    LinearGradient(gradient: .init(colors: [.blue, .green]), startPoint: .top, endPoint: .bottom)
        .cornerRadius(20)
)
0 голосов
/ 07 ноября 2019

Попробуйте использовать .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)
}
...