In this sample I want to clear the value of a TextField with a custom button. However, the button action (value = nil) seems to be ignored. Why?
Tested on Xcode 14 beta 6 (iOS 16).
struct ContentView: View {
@State private var value: Int?
@FocusState var focused: Bool
var buttonOpacity: Double {
focused && value != nil ? 1 : 0
}
var body: some View {
Form {
HStack {
TextField("Value", value: $value, format: .number.grouping(.never))
.focused($focused)
.keyboardType(.numberPad)
Spacer()
Button {
value = nil
} label: {
Label("Erase", systemImage: "multiply")
.labelStyle(.iconOnly)
.imageScale(.medium)
.symbolVariant(.circle.fill)
.symbolRenderingMode(.hierarchical)
.foregroundStyle(.secondary)
}
.buttonStyle(.plain)
.opacity(buttonOpacity)
}
}
}
}