Home > other >  In SwiftUI how to focus soft keypad on Simulator and Preview (it works on hardware)?
In SwiftUI how to focus soft keypad on Simulator and Preview (it works on hardware)?

Time:08-30

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
                         }
                     }
                 }
             }
         }
     }
 }

CodePudding user response:

For this to happen the following Simulator settings apply

Simulator, I/O, Keyboard, Connect Hardware Keyboard

When the app is built and run, Connect Hardware Keyboard is checked by default. In which case Command k key will cause the software keyboard to be displayed. The app will operate on keystrokes either from the software keyboard or the qwerty keyboard. But if the user enters a value via the qwerty keyboard, this will immediately dismiss the software keyboard.

However, if Connect Hardware Keyboard is unchecked, the numeric keypad appears on the screen. The Qwerty keyboard is unresponsive and the on-screen numeric keypad offers the user only means of entering numeric values into the TextField via the Simulator.

  • Related