This minimal code demonstrates a problem I'm having. As you adjust the Slider
(high precision numbers), the TextField
sets the model, changing the number's precision to its format.
But I only want it to use the 3-digit fraction for display, or if the user edits in the field. It shouldn't be mutating the model just because it's displaying the value, no?
Is it a bug? It doesn't seem right to me.
struct ContentView: View {
@State var number: Double = 0
// wrap binding to log the `set` calls
var textFieldBinding: Binding<Double> {
Binding { number } set: {
print("Setting from TextField: \($0)")
number = $0
}
}
var sliderBinding: Binding<Double> {
Binding { number } set: {
print("Setting from Slider: \($0)")
number = $0
}
}
var body: some View {
VStack {
TextField("Number", value: textFieldBinding, format: .number.precision(.fractionLength(3)))
Slider(value: sliderBinding, in: 0...5.0)
}.frame(maxWidth: 300)
}
}
When you drag the slider, you see stuff like:
Setting from Slider: 1.0073260217905045
Setting from TextField: 1.007
...