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

iOS 17 Beta 3键盘遮挡SwiftUI TextField问题求助

iOS 17 Beta 3 中 SwiftUI TextField 被键盘遮挡的修复方案

临时修复方案

  • 用 ScrollView 包裹内容并指定键盘 Dismiss 策略:
    iOS 17 Beta 中自动滚动适配逻辑存在异常,显式配置滚动视图的键盘交互策略,确保输入框能随键盘弹出自动滚动到可视区域:

    ScrollView {
        TextField("输入内容", text: $inputText)
            .padding()
            .background(Color.white)
            .cornerRadius(8)
    }
    .scrollDismissesKeyboard(.interactively)
    
  • 手动监听键盘通知调整布局:
    回归传统键盘通知监听方式,手动计算键盘高度来动态调整输入框底部间距,规避Beta版本的自动适配bug:

    @State private var keyboardHeight: CGFloat = 0
    
    var body: some View {
        VStack {
            // 页面其他内容
            TextField("输入内容", text: $inputText)
                .padding()
                .background(Color.white)
                .cornerRadius(8)
        }
        .padding(.bottom, keyboardHeight)
        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)) { notification in
            guard let frame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
            keyboardHeight = frame.height
        }
        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)) { _ in
            keyboardHeight = 0
        }
    }
    
  • 使用 safeAreaInset 动态调整底部安全区:
    针对iOS 16+ 适配的 safeAreaInset,在Beta版本中需显式绑定键盘高度,确保输入框不被遮挡:

    @State private var keyboardHeight: CGFloat = 0
    
    var body: some View {
        VStack {
            TextField("输入内容", text: $inputText)
                .padding()
                .background(Color.white)
                .cornerRadius(8)
        }
        .safeAreaInset(edge: .bottom) {
            Color.clear.frame(height: keyboardHeight)
        }
        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)) { notification in
            guard let frame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
            keyboardHeight = frame.height
        }
        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)) { _ in
            keyboardHeight = 0
        }
    }
    

额外说明

该问题属于iOS 17 Beta版本的已知兼容性bug,苹果后续大概率会在正式版或后续Beta迭代中修复,建议同步关注官方更新日志;测试时需优先在真实设备上验证修复效果,模拟器的键盘行为与真实设备存在差异。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 08:17:04