在某些情况下尝试自动调整大小时,键盘扩展在iOS 10中会丢失高度

Dim*_*ima 6 cocoa-touch ios autolayout ios-keyboard-extension ios10

您可以在此下载一个示例项目来演示以下问题:https: //github.com/DimaVartanian/keyboard-extension-height-bug

当创建一个键盘的扩展,而不是指定一个具体的高度为它的组成部分,但不是将它们固定到视图/ inputView于是在理论上系统将确定基于环境和方向的高度,在这样的高度,而不是变成0,某些情况下,键盘被压碎(除了具有混凝土高度的任何东西,例如自我尺寸的标签或按钮).

这似乎只发生在iOS 10上.在iOS 9上,子视图正确调整大小以适应默认的自动键盘高度.

这可以表现出几个场景,这个项目演示了一个基本的场景.它从基本的键盘扩展模板开始,带有默认的"下一个键盘"按钮和它附带的2个大小限制:

self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
Run Code Online (Sandbox Code Playgroud)

接下来,我们创建一个其他视图,我们想要填充superview的空间,而不为自己定义具体的大小:

let anotherView = UIView()
anotherView.backgroundColor = UIColor.red
anotherView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(anotherView)
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
Run Code Online (Sandbox Code Playgroud)

现在,假设我们只想将这个新视图锚定到键盘超级视图的底部.我们会做的事情如下:

anotherView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
Run Code Online (Sandbox Code Playgroud)

结果如下:

iOS 9 在此输入图像描述

iOS 10 在此输入图像描述

这种布局正是我们所期望的.现在,让我们将新视图锚定到下一个键盘按钮的顶部.我们摆脱了刚刚添加的约束并替换它

anotherView.bottomAnchor.constraint(equalTo: self.nextKeyboardButton.topAnchor).isActive = true
Run Code Online (Sandbox Code Playgroud)

逻辑上,结果高度应该相同(由系统确定)

结果现在是这样的:

iOS 9 在此输入图像描述

iOS 10 在此输入图像描述

在iOS 9上,它的行为符合预期,但在iOS 10上,灵活的高度视图大小调整为0,剩下的就是固定高度按钮.

没有关于冲突约束的消息.我试图找出可能导致这种情况的原因以及为什么它只会发生在iOS 10上.

Dim*_*ima 3

苹果已经回复了我的 DTS 请求并告诉我提交错误报告,所以这实际上是 iOS 10 的错误。我已经提交了雷达(#28532959),如果收到回复,我将更新此答案。如果其他人提出了一个具体的解决方案,允许我仍然使用自动布局来实现自动高度,答案仍然被接受。