iPhone键盘折叠实现咨询:Swift中按返回键仅折叠而非完全收起键盘
如何在iPhone上实现键盘折叠(而非完全收起)
嘿,我明白你的需求——你不想让键盘彻底消失,而是想把它折叠成那种浮动的小键盘,对吧?下面给你几种可行的方案,从系统原生API到自定义实现都有:
方案1:用iOS 14+的系统API直接触发浮动键盘
苹果从iOS 14开始,提供了直接切换键盘到浮动模式的API,不需要用户手动长按拖动。你可以在代码里这样实现:
// 确保当前有活跃的输入框(比如UITextField/UITextView是第一响应者) guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, let activeInputMode = UITextInputMode.activeInputMode else { print("没有活跃的输入场景或输入法") return } // 请求更新界面几何结构,然后切换到浮动样式 windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait)) { error in if let error = error { print("切换失败:\(error.localizedDescription)") return } activeInputMode.visualStyle = .floating }
不过要注意:这个API的行为在不同设备(比如SE和Pro Max)上可能略有差异,而且必须保证当前有正在编辑的输入控件,否则会无效。
方案2:自定义折叠键盘视图
如果系统的浮动键盘不符合你的UI需求,你可以自己做一个折叠状态的小键盘。思路是:隐藏系统键盘,然后显示自己做的简化版键盘,同步输入内容到目标输入框。
给你个简单的示例代码:
// 自定义折叠键盘视图 class FoldedKeyboard: UIView { weak var targetTextField: UITextField? override init(frame: CGRect) { super.init(frame: frame) setupUI() } required init?(coder: NSCoder) { super.init(coder: coder) setupUI() } private func setupUI() { backgroundColor = .systemGray6 layer.cornerRadius = 12 // 添加常用按键,这里以空格和删除键为例 let spaceBtn = UIButton(type: .system) spaceBtn.setTitle("空格", for: .normal) spaceBtn.titleLabel?.font = .systemFont(ofSize: 16) spaceBtn.addTarget(self, action: #selector(appendSpace), for: .touchUpInside) let deleteBtn = UIButton(type: .system) deleteBtn.setTitle("删除", for: .normal) deleteBtn.titleLabel?.font = .systemFont(ofSize: 16) deleteBtn.addTarget(self, action: #selector(deleteLastChar), for: .touchUpInside) // 布局按键(这里用AutoLayout更规范,为了简洁用frame示例) spaceBtn.frame = CGRect(x: 20, y: 10, width: bounds.width - 120, height: 40) deleteBtn.frame = CGRect(x: bounds.width - 90, y: 10, width: 70, height: 40) addSubview(spaceBtn) addSubview(deleteBtn) } @objc private func appendSpace() { targetTextField?.text?.append(" ") } @objc private func deleteLastChar() { targetTextField?.text = targetTextField?.text?.dropLast().string } } // 在ViewController里使用 class DemoVC: UIViewController { let inputField = UITextField() let foldedKeyboard = FoldedKeyboard(frame: CGRect(x: 20, y: 0, width: UIScreen.main.bounds.width - 40, height: 60)) override func viewDidLoad() { super.viewDidLoad() setupInputField() } private func setupInputField() { inputField.borderStyle = .roundedRect inputField.frame = CGRect(x: 20, y: 100, width: view.bounds.width - 40, height: 40) // 添加一个工具栏,放折叠按钮 let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)) let foldBtn = UIBarButtonItem(title: "折叠键盘", style: .plain, target: self, action: #selector(toggleFoldedKeyboard)) toolbar.items = [UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), foldBtn] inputField.inputAccessoryView = toolbar view.addSubview(inputField) } @objc private func toggleFoldedKeyboard() { if inputField.isFirstResponder { // 收起系统键盘,显示自定义折叠键盘 inputField.resignFirstResponder() foldedKeyboard.targetTextField = inputField foldedKeyboard.center = CGPoint(x: view.center.x, y: view.bounds.height - 80) view.addSubview(foldedKeyboard) } else { // 隐藏自定义键盘,唤起系统键盘 foldedKeyboard.removeFromSuperview() inputField.becomeFirstResponder() } } }
方案3:让用户手动触发系统折叠键盘
其实iPhone原生就支持手动折叠键盘,不需要写代码:用户只需要长按键盘右下角的那个小键盘图标(带向下箭头的那个),然后轻轻拖动键盘,就能把它变成浮动的小键盘;再次拖动或者点击图标就能恢复全屏。如果你的需求只是让用户知道这个功能,直接告诉用户这个操作就行啦!
内容的提问来源于stack exchange,提问作者mfaani
相关产品推荐
相关产品推荐

