Hey guys, here is what worked for me:
import UIKit
final class KeyboardViewController: UIInputViewController {
private var heightConstraint: NSLayoutConstraint?
private var viewWillAppearWasCalled = false
override func viewDidLoad() {
super.viewDidLoad()
// Create early, but do NOT activate yet
heightConstraint = inputView?.heightAnchor.constraint(equalToConstant: preferredKeyboardHeight())
// Important: use required - 1 to avoid fighting UIKit's own host constraints.
heightConstraint?.priority = UILayoutPriority.required - 1
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewWillAppearWasCalled = true
}
override func updateViewConstraints() {
super.updateViewConstraints()
// Important: activate height constraint after viewWillAppear
guard viewWillAppearWasCalled,
let inputView,
let heightConstraint
else {
return
}
heightConstraint.constant = preferredKeyboardHeight(for: inputView.bounds.size)
if !heightConstraint.isActive {
heightConstraint.isActive = true
}
}
private func preferredKeyboardHeight(for size: CGSize? = nil) -> CGFloat {
guard let size else {
// In viewDidLoad size is nil
return 256 // Some initial value
}
return 256 // Some custom value depending on size
}
}
Hope that helps :)
Topic:
UI Frameworks
SubTopic:
UIKit