Post

Replies

Boosts

Views

Activity

Reply to Different keyboards show up for KeyboardType .decimalPad
First of all, thank you very much for replying on my thread :) I have prepared two code snippets: The first one is a very simple one that should demonstrate my problem pretty straightforward struct ContentView: View { @State private var text: String = "" var body: some View { Form { Section { LabeledContent( content: { TextField("Text", text: $text) .keyboardType(.decimalPad) }, label: { Text("Text") } ) } } } } A more sophisticated approach from my project where i specifically saw the problem (as both show the same behavior, i think both might be related) import SwiftUI struct Area { var length: Double? var width: Double? var areaString: String { guard let l = length, let w = width else { return "-" } return String(format: "%.1f cm²", l * w) } } private enum AreaAttribute: Hashable { case length, width var label: LocalizedStringKey { switch self { case .length: "Length" case .width: "Width" } } } struct ContentView: View { @State private var area: Area = Area() @FocusState private var focused: AreaAttribute? var body: some View { Form { Section { AreaRow(value: $area.length, label: .length, focused: $focused) AreaRow(value: $area.width, label: .width, focused: $focused) } Section { LabeledContent("Area", value: area.areaString) } } } } private struct AreaRow: View { @Binding var value: Double? @State private var text = "" @State private var isEditing = false let label: AreaAttribute var focused: FocusState<AreaAttribute?>.Binding var body: some View { LabeledContent(label.label) { if isEditing { HStack { TextField("", text: $text) .keyboardType(.decimalPad) .multilineTextAlignment(.trailing) .focused(focused, equals: label) .onChange(of: text) { _, new in value = Double(new.replacingOccurrences(of: ",", with: ".")) } Text("cm").foregroundStyle(.secondary) } } else { Button("Add") { isEditing = true focused.wrappedValue = label } } } .onAppear { if let value { text = String(format: "%.1f", value) isEditing = true } } } } So basically what happens (I am explaining it now regarding the second code sample) I click on Add -> Floating decimal pad opens up -> i enter a number -> I click outside the decimalPad -> (Textfield stays focussed) -> I click again on the TextField -> Keyboard from bottom slides over (with numbers and special characters). I don't explicitly have a .onTapGesture in the background as (from my understanding) one could rely on the dismissal of the keyboard to automatically reset the FocusState. Hope that helps and thank you again!!
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’26
Reply to Different keyboards show up for KeyboardType .decimalPad
First of all, thank you very much for replying on my thread :) I have prepared two code snippets: The first one is a very simple one that should demonstrate my problem pretty straightforward struct ContentView: View { @State private var text: String = "" var body: some View { Form { Section { LabeledContent( content: { TextField("Text", text: $text) .keyboardType(.decimalPad) }, label: { Text("Text") } ) } } } } A more sophisticated approach from my project where i specifically saw the problem (as both show the same behavior, i think both might be related) import SwiftUI struct Area { var length: Double? var width: Double? var areaString: String { guard let l = length, let w = width else { return "-" } return String(format: "%.1f cm²", l * w) } } private enum AreaAttribute: Hashable { case length, width var label: LocalizedStringKey { switch self { case .length: "Length" case .width: "Width" } } } struct ContentView: View { @State private var area: Area = Area() @FocusState private var focused: AreaAttribute? var body: some View { Form { Section { AreaRow(value: $area.length, label: .length, focused: $focused) AreaRow(value: $area.width, label: .width, focused: $focused) } Section { LabeledContent("Area", value: area.areaString) } } } } private struct AreaRow: View { @Binding var value: Double? @State private var text = "" @State private var isEditing = false let label: AreaAttribute var focused: FocusState<AreaAttribute?>.Binding var body: some View { LabeledContent(label.label) { if isEditing { HStack { TextField("", text: $text) .keyboardType(.decimalPad) .multilineTextAlignment(.trailing) .focused(focused, equals: label) .onChange(of: text) { _, new in value = Double(new.replacingOccurrences(of: ",", with: ".")) } Text("cm").foregroundStyle(.secondary) } } else { Button("Add") { isEditing = true focused.wrappedValue = label } } } .onAppear { if let value { text = String(format: "%.1f", value) isEditing = true } } } } So basically what happens (I am explaining it now regarding the second code sample) I click on Add -> Floating decimal pad opens up -> i enter a number -> I click outside the decimalPad -> (Textfield stays focussed) -> I click again on the TextField -> Keyboard from bottom slides over (with numbers and special characters). I don't explicitly have a .onTapGesture in the background as (from my understanding) one could rely on the dismissal of the keyboard to automatically reset the FocusState. Hope that helps and thank you again!!
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’26