Custom Keyboard help

import UIKit

class KeyboardViewController: UIInputViewController {

// MARK: - Properties
private var keyboardView: KeyboardView!
private var heightConstraint: NSLayoutConstraint!
private var hasInitialLayout = false

// 存储系统键盘高度和动画参数
private var systemKeyboardHeight: CGFloat = 300
private var keyboardAnimationDuration: Double = 0.25
private var keyboardAnimationCurve: UIView.AnimationOptions = .curveEaseInOut

// MARK: - Lifecycle
override func viewDidLoad() {
    super.viewDidLoad()
    setupKeyboard()
    
    
}



override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    // 在视图显示前更新键盘高度,避免闪动
    if !hasInitialLayout {
        hasInitialLayout = true
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
}

// MARK: - Setup
private func setupKeyboard() {
    // 创建键盘视图
    keyboardView = KeyboardView()
    keyboardView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(keyboardView)
    
    // 设置约束 - 确保键盘贴紧屏幕底部
    NSLayoutConstraint.activate([
        keyboardView.leftAnchor.constraint(equalTo: view.leftAnchor),
        keyboardView.rightAnchor.constraint(equalTo: view.rightAnchor),
        keyboardView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ])
    
    // 设置初始高度约束(使用系统键盘高度或默认值)
    let initialHeight = systemKeyboardHeight
    heightConstraint = keyboardView.heightAnchor.constraint(equalToConstant: initialHeight)
    heightConstraint.isActive = true
}





// MARK: - Layout Events
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
}

override func viewSafeAreaInsetsDidChange() {
    super.viewSafeAreaInsetsDidChange()
}

// MARK: - 键盘高度请求
// 这个方法可以确保键盘扩展报告正确的高度给系统
override func updateViewConstraints() {
    super.updateViewConstraints()
    
    // 确保我们的高度约束是最新的
    if heightConstraint == nil {
        let height = systemKeyboardHeight > 0 ? systemKeyboardHeight : 216
        heightConstraint = NSLayoutConstraint(
            item: self.view!,
            attribute: .height,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 0.0,
            constant: height
        )
        heightConstraint.priority = UILayoutPriority(999)
        view.addConstraint(heightConstraint)
    } else {
        let height = systemKeyboardHeight > 0 ? systemKeyboardHeight : 216
        heightConstraint.constant = height
    }
}

}

// MARK: - Keyboard View Implementation class KeyboardView: UIView {

private var keysContainer: UIStackView!

override init(frame: CGRect) {
    super.init(frame: frame)
    setupView()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    setupView()
}

private func setupView() {
    backgroundColor = UIColor(red: 0.82, green: 0.84, blue: 0.86, alpha: 1.0)
    
    // 创建按键容器
    keysContainer = UIStackView()
    keysContainer.axis = .vertical
    keysContainer.distribution = .fillEqually
    keysContainer.spacing = 8
    keysContainer.translatesAutoresizingMaskIntoConstraints = false
    addSubview(keysContainer)
    
    // 添加约束 - 确保内容在安全区域内
    NSLayoutConstraint.activate([
        keysContainer.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 8),
        keysContainer.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor, constant: 8),
        keysContainer.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor, constant: -8),
        keysContainer.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -8)
    ])
    
    // 添加键盘行
}

}

Custom Keyboard help
 
 
Q