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)
    }
}
相关推荐
东坡肘子2 小时前
苹果的罕见妥协:当高危漏洞遇上“拒升”潮 -- 肘子的 Swift 周报 #130
人工智能·swiftui·swift
zhensherlock1 天前
Protocol Launcher 系列:1Writer iOS 上的 Markdown 文档管理
javascript·笔记·ios·typescript·node.js·iphone·ipad
ˇasushiro1 天前
终端工具配置
开发语言·ios·swift
CDN3601 天前
iOS/Android 集成游戏盾审核被拒?权限与合规配置修复
android·游戏·ios
sun03221 天前
旧版Ipad无法访问https的原因(不支持TLS1.3)
ios·https·ipad
humors2212 天前
[原创]AI工具:读取手机系统文件工具
windows·ios·安卓·鸿蒙·文件·苹果·读取
humors2212 天前
[原创]AI工具:手机文件查杀病毒工具
windows·ios·手机·安卓·鸿蒙·杀毒·苹果
2501_915918413 天前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
左左右右左右摇晃3 天前
Tasker笔记
ios·iphone