Dima Dima - 1 year ago 107
iOS Question

Keyboard extension loses height in iOS 10 when trying to size automatically in some cases

You can download a sample project demonstrating the issue below here:

When creating a keyboard extension and not specifying a concrete height for its components but instead anchoring them to the view/inputView so that in theory the system will determine their height based on environment and orientation, in some situations that height instead turns into 0 and the keyboard is crushed (with the exception of anything that has a concrete height such as a self sized label or button).

This only seems to occur on iOS 10. On iOS 9, the child views resized correctly to fit the default automatic keyboard height.

There are several scenarios this can manifest and this project demonstrates a basic one. It starts with the basic keyboard extension template with the default "next keyboard" button and the 2 size constraints it comes with:

self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

Next, we create a single other view that we want to fill the space of the superview without defining a concrete size for itself:

let anotherView = UIView()
anotherView.backgroundColor = UIColor.red
anotherView.translatesAutoresizingMaskIntoConstraints = false
anotherView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
anotherView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
anotherView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true

Now, let's say we just want to anchor this new view to the bottom of our keyboard superview. We would just do something like:

anotherView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

The result looks like this:

iOS 9
enter image description here

iOS 10
enter image description here

This layout is exactly what we expect. Now instead, let's anchor the new view to the top of our next keyboard button. We get rid of the constraint we just added and replace it with

anotherView.bottomAnchor.constraint(equalTo: self.nextKeyboardButton.topAnchor).isActive = true

Logically, the resulting height should be the same (determined by the system)

The result is now this:

iOS 9
enter image description here

iOS 10
enter image description here

On iOS 9 it behaves as expected but on iOS 10, the flexible height view is resized down to 0 and all that is left is the fixed height button.

There are no messages about conflicting constraints. I'm trying to figure out what could be causing this and why it would only be happening on iOS 10.

Answer Source

Apple has responded to my DTS ticket and told me to file a bug report, so this is actually an iOS 10 bug. I have filed a radar (#28532959) and will update this answer if I ever get a response. If someone else comes up with a concrete solution that allows me to still use autolayout to achieve an automatic height, answers are still accepted.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download