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)
    ])
    
    // 添加键盘行
}

}

I don't see you described what doesn't work... For folks to help, you might consider providing more context about your question, for example, what the issue is, and how you trigger the issue. See tips on writing forums posts, if necessary.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

The above is a very simple code. When I switch from the system keyboard to the extended keyboard, the extended keyboard will flash. The first switch will not flash, and the subsequent keyboard switches will flash. This is more obvious on phones with a safe area at the bottom of the screen. Thank you.

I play with an iOS app + a custom keyboard extension created with the project templates Xcode provides, without adding any code, on my iOS 26 device, and do see that when switching to the custom keyboard, the keyboard has a flash, which seems to be triggered because the system gives the keyboard a bigger initial height, and then change it after the keyboard appears on the screen.

I unfortunately don’t see anything that can fix the issue. A keyboard extension runs in a separate process. The system uses an internal mechanism to render the keyboard with the hosting iOS app. As a result, the extension can only change the height of the keyboard AFTER its primary view initially draws on screen. There is no way to avoid the flash by adjusting the height of the keyboard before it appears on screen.

Based on that, I can only suggest that you file a feedback report for the relevant team to investigate – If you do so, please share your report ID.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Custom Keyboard help
 
 
Q