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

iOS开发:如何让inputAccessoryView在键盘隐藏时不消失?

解决inputAccessoryView键盘消失时不隐藏的问题

嘿,我来帮你搞定这个问题!你之前尝试修改frame的思路走不通,核心原因是**inputAccessoryView是和键盘的生命周期强绑定的**——系统会在键盘隐藏时自动把它从视图层级里移除,你修改frame的操作会被系统的后续处理直接覆盖掉,自然看不到效果。

正确的解决方案

我们换个思路:不依赖系统的inputAccessoryView,自己创建一个底部视图,通过约束控制它的位置,同时监听键盘状态来动态调整,这样键盘消失后视图依然能留在屏幕底部,还能模拟inputAccessoryView的交互效果。

代码实现

import UIKit

class YourViewController: UIViewController {
    @IBOutlet weak var textview: UITextView!
    // 保存底部约束的引用,方便后续修改
    private var customViewBottomConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
        setupCustomBottomView()
        setupKeyboardNotifications()
    }

    private func setupCustomBottomView() {
        // 创建自定义底部视图
        let customView = UIView()
        customView.backgroundColor = .white
        customView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(customView)

        // 设置视图约束:左右贴边、固定高度88
        NSLayoutConstraint.activate([
            customView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            customView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            customView.heightAnchor.constraint(equalToConstant: 88)
        ])

        // 底部约束初始贴主视图底部
        customViewBottomConstraint = customView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        customViewBottomConstraint.isActive = true

        // 给textView设置一个空的inputAccessoryView,避免系统默认的空白区域
        let emptyAccessory = UIView(frame: .zero)
        emptyAccessory.isHidden = true
        textview.inputAccessoryView = emptyAccessory
    }

    private func setupKeyboardNotifications() {
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(keyboardWillShow),
            name: UIResponder.keyboardWillShowNotification,
            object: nil
        )
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(keyboardWillHide),
            name: UIResponder.keyboardWillHideNotification,
            object: nil
        )
    }

    @objc private func keyboardWillShow(sender: NSNotification) {
        guard let userInfo = sender.userInfo,
              let keyboardFrame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
        
        // 调整底部约束,让视图贴在键盘顶部
        customViewBottomConstraint.constant = -keyboardFrame.height
        // 加动画和系统键盘动画保持一致
        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }

    @objc private func keyboardWillHide(sender: NSNotification) {
        // 恢复底部约束,让视图回到屏幕底部
        customViewBottomConstraint.constant = 0
        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }

    deinit {
        // 记得移除通知,避免内存泄漏
        NotificationCenter.default.removeObserver(self)
    }
}

关键说明

  • 我们直接把自定义视图添加到主视图上,通过约束完全掌控它的位置,不再依赖系统的inputAccessoryView生命周期。
  • 给textView设置空的inputAccessoryView是为了避免系统在键盘弹出时自动添加默认的空白区域,保证布局的一致性。
  • 用动画更新约束,让视图的移动和键盘的弹出/收起动画同步,体验更流畅。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:30:50