I have nothing but praise for Paul Hudson's excellent Hacking With Swift tutorials but when I build and run Tutorial 10, the soft keypad only comes into focus on hardware (an iPhone 12 running iOS 15.6.1) but not on Simulator or Xcode Preview. The following message appears in the Console area.
Can't find keyplane that supports type 8 for keyboard iPhone-PortraitChoco-DecimalPad; using 27100_PortraitChoco_iPhone-Simple-Pad_Default
On the Simulator the Done button appears at the bottom of the screen but does not appear on the Preview. I found similar issues pre-dating SwiftUI here and here. The most likely cause of the problem is that I have recreated the code incorrectly.
I am running Xcode 13.4.1 on MacOS 12.5.
Does someone else have the same problem running my code ? if not, what version of Xcode have you used ?
import SwiftUI
// Tutorial 10
struct ContentView: View {
@State private var checkAmount = 0.0
@State private var numberOfPeople = 2
@State private var tipPercentage = 20
@FocusState private var amountIsFocused: Bool
let tipPercentages = [10, 15, 20, 25, 0]
var totalPerPerson: Double {
let peopleCount = Double(numberOfPeople + 2)
let tipSelection = Double(tipPercentage)
let tipValue = checkAmount / 100 * tipSelection
let grandTotal = checkAmount + tipValue
let amountPerPerson = grandTotal / peopleCount
return amountPerPerson
}
var body: some View {
NavigationView {
Form {
Section {
TextField("Amount", value: $checkAmount, format: .currency(code: Locale.current.currencyCode ?? "USD"))
.keyboardType(.decimalPad)
.focused($amountIsFocused)
Picker("Number of people", selection: $numberOfPeople) {
ForEach(2 ..< 100) {
Text("\($0) people")
}
}
}
Section {
Picker("Tip percentage", selection: $tipPercentage) {
ForEach(tipPercentages, id: \.self) {
Text($0, format: .percent)
}
}
.pickerStyle(.segmented)
} header: {
Text("How much tip do you want to leave?")
}
Section {
Text(totalPerPerson, format: .currency(code: Locale.current.currencyCode ?? "USD"))
}
.navigationTitle("WeSplit")
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Button("Done") {
amountIsFocused = false
}
}
}
}
}
}
}