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

SwiftUI:ViewThatFits结合TextField在HStack布局下无法唤起键盘

SwiftUI中ViewThatFits与TextField的布局异常及键盘唤起问题

当ViewThatFits与TextField配合使用时,会出现两个异常现象:

  • 本该显示HStack布局的场景下,SwiftUI仍保留VStack布局的状态
  • 仅在VStack布局显示时,点击TextField才能正常唤起软键盘;切换到HStack布局后,点击TextField无法唤起键盘,控制台会输出以下错误日志:

-[RTIInputSystemClient remoteTextInputSessionWithID:performInputOperation:] perform input operation requires a valid sessionID. inputModality = Keyboard, inputOperation = , customInfoType = UIEmojiSearchOperations

复现问题的示例代码如下:

struct ContentView: View {
    @State var text1 = ""
    @State var text2 = ""

    @ViewBuilder var content: some View {
        Image(systemName: "globe")
            .font(.system(size: 100))
        Text("Hello")
            .font(.system(size: 50))
    }
    
    var textfields: some View {
        VStack {
            TextField("text1", text: $text1)
            TextField("text2", text: $text2)
        }
        .textFieldStyle(.roundedBorder)
    }
    
    var body: some View {
        ViewThatFits {
            VStack {
                content
                textfields
            }
            
            HStack {
                content
                textfields
            }
        }
        .padding()
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 14:35:56