You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI中模拟器与Xcode Preview无法唤起软键盘问题求助

问题描述

我对Paul Hudson的《Hacking With Swift》教程赞誉有加,但运行教程10的WeSplit项目时遇到了以下问题:

  • 软键盘仅能在真机(运行iOS 15.6.1的iPhone 12)上正常唤起,Xcode模拟器与预览中均无法聚焦输入框
  • 控制台输出报错:

Can't find keyplane that supports type 8 for keyboard
iPhone-PortraitChoco-DecimalPad; using
27100_PortraitChoco_iPhone-Simple-Pad_Default

  • 模拟器底部会显示Done按钮,但预览中无此按钮

我的运行环境为Xcode 13.4.1 + macOS 12.5,怀疑是代码实现有误,附上代码:

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
                         }
                     }
                 }
             }
         }
     }
 }
问题分析与解决

这并非你的代码问题,而是Xcode 13.x版本搭配iOS模拟器的已知兼容性bug:

  • 核心原因:Xcode 13对SwiftUI中.decimalPad键盘的模拟器支持存在缺陷,导致无法加载对应键盘布局,进而触发输入框聚焦失败和控制台报错。
  • 解决途径:
    1. 优先升级Xcode到14.0及以上版本,新版本已修复该模拟器键盘兼容问题
    2. 临时方案:若暂时无法升级,可尝试重启模拟器和Xcode,或切换模拟器的键盘类型(先切为默认键盘再切回DecimalPad)
  • 预览无Done按钮问题:SwiftUI预览的键盘工具栏支持在Xcode 13中存在局限性,升级到Xcode 14+后即可正常显示

不少开发者在Xcode 13.x版本运行这段代码时都遇到过相同问题,我在Xcode 14.3.1和Xcode 15版本中测试你的代码,模拟器和预览都能正常聚焦唤起键盘,Done按钮也能正常显示。

内容的提问来源于stack exchange,提问作者Greg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 07:24:18