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

iOS键盘高度调整方法咨询及Decimal Pad键盘高度适配问题

如何调整iOS中Decimal Pad键盘的高度?

你说的Decimal Pad和默认键盘存在高度差的问题我也碰到过,不过得先给你提个醒:iOS系统不允许直接修改原生系统键盘的高度——苹果对系统键盘的样式和尺寸管控很严,硬改不仅可能在不同系统版本出兼容问题,还可能过不了App Store审核。

看你的代码,你是想通过调整自身视图的约束来适配,这个思路是对的,不过现有代码还有几个可以优化的地方,咱们一步步来改:

首先说下你当前代码的问题:

  • 不管键盘显示还是隐藏,你都把desiredHeight设为260,这会导致键盘收起后视图的约束还是260,明显不符合预期
  • 直接硬写260的高度,没考虑不同设备的键盘高度差异,也没区分Decimal Pad和普通键盘的原始高度

接下来给你两个可行的方案:


方案1:动态适配视图约束(不修改系统键盘)

我们可以先获取当前键盘的原始高度,再根据键盘类型(Decimal Pad)调整我们的视图约束,这样既适配了不同设备,也不会碰系统键盘的红线:

// 先在类里存一下原始键盘高度
var originalKeyboardHeight: CGFloat = 0

@objc func resizeKeyboard(note: NSNotification) {
    // 先把userInfo里的参数安全取出来
    guard let userInfo = note.userInfo,
          let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double,
          let curveValue = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt,
          let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else {
        return
    }
    
    let frameEnd = self.view.convert(keyboardFrame.cgRectValue, from: nil)
    originalKeyboardHeight = frameEnd.height
    
    let animationOptions = UIView.AnimationOptions(rawValue: curveValue)
    
    if note.name == UIResponder.keyboardWillShowNotification {
        // 判断当前激活的输入框是不是用的Decimal Pad
        if let activeTextField = UIApplication.shared.windows.first?.firstResponder as? UITextField,
           activeTextField.keyboardType == .decimalPad {
            // 这里可以自定义调整后的高度,比如比原始高度少20pt
            let adjustedHeight = originalKeyboardHeight - 20
            self.heightConstraint.constant = adjustedHeight
        } else {
            // 普通键盘就用原始高度适配
            self.heightConstraint.constant = originalKeyboardHeight
        }
    } else if note.name == UIResponder.keyboardWillHideNotification {
        // 键盘隐藏时把约束改回默认值(比如0,根据你的布局来)
        self.heightConstraint.constant = 0
    }
    
    // 执行动画,和键盘动画保持一致
    UIView.animate(withDuration: duration, delay: 0, options: animationOptions) {
        self.view.layoutIfNeeded()
    }
}

配套的设置步骤

  • 在viewDidLoad里注册键盘通知:
    override func viewDidLoad() {
        super.viewDidLoad()
        // 注册键盘通知
        NotificationCenter.default.addObserver(self, selector: #selector(resizeKeyboard(note:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(resizeKeyboard(note:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
  • 在deinit里移除通知,避免内存泄漏:
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    

方案2:自定义Decimal Pad键盘(完全可控)

如果你对键盘高度有严格要求,最稳妥的方式是自己实现一个自定义键盘,这样你能完全控制它的尺寸、样式和交互,而且完全符合苹果的规范:

比如我们写一个自定义键盘类:

class CustomDecimalPad: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .systemGray5
        
        // 这里可以布局你的数字按钮、小数点按钮、退格键等
        // 比如用UIStackView来排列按钮,实现类似系统Decimal Pad的布局
        
        // 直接设置你想要的高度
        self.frame.size.height = 260
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 这里可以添加按钮点击事件,处理输入逻辑
    @objc func buttonTapped(_ sender: UIButton) {
        // 把输入内容传递给当前的输入框
        if let textField = UIApplication.shared.windows.first?.firstResponder as? UITextField {
            textField.text?.append(sender.titleLabel?.text ?? "")
        }
    }
}

然后给你的输入框设置这个自定义键盘:

let decimalTextField = UITextField()
// 设置自定义输入视图
decimalTextField.inputView = CustomDecimalPad(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 260))

这种方式完全避开了系统键盘的限制,你想把键盘做成什么高度、什么样式都可以,也不用担心审核问题,推荐在对键盘有定制化需求的场景下使用。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:48:58