iOS / SwiftUI 输入法(键盘)布局处理总结(AI版)

文章目录

📘 iOS / SwiftUI 输入法(键盘)处理总结


一、问题背景

在 iOS / SwiftUI 开发中,常见输入法问题:

  • 输入框切换时键盘闪烁
  • 键盘弹出/收起动画不自然
  • 无法像 Android WindowInsetsAnimation 一样平滑跟随

二、输入框切换闪烁问题

❌ 错误原因

swift 复制代码
focusedField = nil
focusedField = .password

解决办法

swift 复制代码
enum Field {
    case username
    case password
}

@FocusState private var focusedField: Field?

TextField("用户名", text: $username)
    .focused($focusedField, equals: .username)

SecureField("密码", text: $password)
    .focused($focusedField, equals: .password)

❗ 注意

不要先设为 nil

不要手动关闭键盘

使用系统焦点切换

键盘动画(类似 Android Insets)

uikit

swift 复制代码
@objc func keyboardWillChangeFrame(_ notification: Notification) {
    guard let userInfo = notification.userInfo else { return }
    
    let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double ?? 0.25
    let curveRaw = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt ?? 0
    let curve = UIView.AnimationOptions(rawValue: curveRaw << 16)
    
    let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect ?? .zero
    let keyboardHeight = UIScreen.main.bounds.height - frame.origin.y
    
    UIView.animate(withDuration: duration, delay: 0, options: curve) {
        self.bottomConstraint.constant = keyboardHeight
        self.view.layoutIfNeeded()
    }
}

swiftUI

  1. 推荐
swift 复制代码
.safeAreaInset(edge: .bottom) {
    Color.clear.frame(height: 0)
}
  1. combine
swift 复制代码
class KeyboardObserver: ObservableObject {
    @Published var height: CGFloat = 0
    
    private var cancellables = Set<AnyCancellable>()
    
    init() {
        NotificationCenter.default.publisher(for: UIResponder.keyboardWillChangeFrameNotification)
            .map { notification -> CGFloat in
                let frame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect ?? .zero
                return UIScreen.main.bounds.height - frame.origin.y
            }
            .assign(to: \.height, on: self)
            .store(in: &cancellables)
    }
}

struct ContentView: View {
    @StateObject var keyboard = KeyboardObserver()
    
    var body: some View {
        VStack {
            Spacer()
            
            TextField("输入", text: .constant(""))
                .textFieldStyle(.roundedBorder)
        }
        .padding(.bottom, keyboard.height)
        .animation(.easeOut(duration: 0.25), value: keyboard.height)
    }
}
相关推荐
for_ever_love__1 天前
UI学习:UICollectionView瀑布流
学习·ui·ios·objective-c·cocoa
大熊猫侯佩1 天前
WWDC26 全网首发:SwiftUI 8 “可重排序“操作符深度解析
ios·swiftui·swift
邓小乐1 天前
Workaround: Xcode27 下载iOS 27.0 Simulator
ios·xcode
韩曙亮1 天前
【Flutter】Flutter 中的 Android / iOS 特殊配置 ① ( 网络权限配置 | HTTP 明文传输配置 | 应用名称配置 )
android·网络·flutter·http·ios·网络权限
人月神话-Lee1 天前
【图像处理】颜色空间——RGB之外的世界
图像处理·人工智能·ios·ai编程·swift·rgb·颜色空间
东坡肘子1 天前
WWDC 2026 初印象:符合预期,但更务实 -- 肘子的 Swift 周报 #139
人工智能·swiftui·swift
CocoaKier1 天前
苹果后台年龄分级填写错误,可能导致审核被拒!
ios·apple
月诸清酒1 天前
Codex 现在能在浏览器里跑 iOS 模拟器了
ios
武子康1 天前
调查研究-159 Apple WWDC 2026 定档 6/8-12:Siri 与 AI 升级,可能是苹果最关键的一次
人工智能·深度学习·ios·ai·chatgpt·apple·wwdc