SwiftUI - 表单选择器和按钮不共存



我创建了一个可重复使用的组件,它在表单中有一个选择器、文本字段和按钮。但是,有了按钮,点击选择器字段就不会转到选择器。相反,它执行按钮代码。TextField工作正常。如果我删除了按钮代码,选择器将出现正确的行为。所以问题是如何在这个组件中同时包含这两个元素?请注意,预览会添加导航和表单,否则这些导航和表单将来自父视图。

var body: some View {
HStack {
if !showFee {
Spacer()
Button(action: {
withAnimation(.easeInOut(duration: 0.3)) {
self.showFee.toggle()
}
}) {
Image(systemName: "plus.circle")
.font(.largeTitle)
}
Spacer()
} else {
VStack(spacing:20) {
Picker(selection: $feeSelection, label: Text("Fee Type")) {
ForEach(0 ..< fees.count) {
Text(self.fees[$0])
}
}
TextField("Fee Amount: $", value: $feeAmount, formatter: NumberFormatter.currency)
.keyboardType(.decimalPad)
Divider()
Button(action: {
withAnimation(.easeInOut(duration: 0.3)) {
self.showFee.toggle()
}
}) {
Image(systemName: "trash.circle.fill")
.font(.largeTitle)
.foregroundColor(.red)
}
}
}
}
.padding()
}
}
struct FeeCell_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
Form {
FeeCell()
}
}
}
}

您可以将PlainButtonStyle应用于按钮。这将停止按钮的点击覆盖表单中的整个单元格:

Button(action: {}) {
Text("Button")
}
.buttonStyle(PlainButtonStyle())

最新更新