Custom Keyboard Extension Keeps Increasing In Height

This custom keyboard I've developed seems fine on an iPhone but on an iPad it gets taller and taller on subsequent switching. Happens in multiple apps. Always happened on a friend's older iPad, recently it now happens on mine too.

I've tried setting constraints and priorities, following the developer documentation, some of which is quite old now.

After hours experimenting, I'm convinced it has nothing to do with the size or properties of what I'm drawing, I'm sure the OS is just giving me a rectangle that starts slightly higher each time.

This animation shows a series of screenshots taken after switching and bringing back the keyboard. (switching not shown)

Sorry the source code is too much to reproduce here, and I don't really know how to simplify it to a minimum example, it's over on Github:

Source Code on Github

Basically, the keyboard is laid out in an InputViewController

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        let viewWidth = self.view.frame.size.width
        let viewHeight = self.view.frame.size.height

        ...stuff gets drawn here

As I said, it doesn't matter how big or small I choose to draw, or what constraints are set, the actual area given in the first place into which I'm laying things out and drawing is wrong.

There is a Similar issue reported by someone on StackOverflow.

Could not resolve this, but have re-written the keyboard extension using KeyboardKit, so the link to Github now will no longer show the code I was referring to.

I came across the same issue and finally resolved it by adding fixed height for the custom keyboard in its viewWillAppear method by adding a height constraint directly to the keyboard view.

  override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let desiredHeight: CGFloat = 250 // Set a default height
        
        // Remove any existing height constraint to avoid conflicts
        view.constraints.forEach { constraint in
            if constraint.firstAttribute == .height {
                view.removeConstraint(constraint)
            }
        }
        
        let heightConstraint = NSLayoutConstraint(
            item: view!,
            attribute: .height,
            relatedBy: .equal,
            toItem: nil,
            attribute: .notAnAttribute,
            multiplier: 1.0,
            constant: desiredHeight
        )
        
        view.addConstraint(heightConstraint)
    }

This code snippet removes any existing height constraint on the keyboard view before adding the new height constraint. It sets a fixed height (desiredHeight) for the keyboard view when it appears. Hope this helps.

Custom Keyboard Extension Keeps Increasing In Height
 
 
Q