更改键盘扩展中的视图高度 (iPhone X)

swi*_*ser 2 swift ios-keyboard-extension swift4 iphone-x

我有以下问题:我构建了一个键盘扩展,这是系统键盘的替代键盘。在所有较旧的 iPhone 型号中,它都可以正常工作,但在 iPhone X 上则不然。我的问题是,键盘视图的高度从 216px (iPhone 8 ,8+ , ...) 减少到 141px (iPhone X)。由于高度较低,我的键盘按钮现在更小,以达到良好的用户可用性。

我确实使用了 .xib 文件来创建 UI,我以编程方式添加所有 UI - 项目。

iPhone 8 iPhone X

我的问题

是否可以为键盘扩展视图获得更多空间(高度)(特别是 iPhone X)?

swi*_*ser 5

我解决我的问题。我在Apple 文档中找到了以下段落:\n

\n
\n

您可以使用自动布局调整自定义键盘\xe2\x80\x99s 主视图的高度。默认情况下,自定义键盘的大小会根据屏幕尺寸和设备方向与系统键盘相匹配。自定义键盘\xe2\x80\x99s 宽度始终由系统设置为等于当前屏幕宽度。要调整自定义键盘\xe2\x80\x99s 高度,请更改其主视图的高度限制。

\n

以下代码行显示了如何定义和添加此类约束:

\n
\n
CGFloat _expandedHeight = 500;\nNSLayoutConstraint *_heightConstraint = \n    [NSLayoutConstraint constraintWithItem: self.view \n                                 attribute: NSLayoutAttributeHeight \n                                 relatedBy: NSLayoutRelationEqual \n                                    toItem: nil \n                                 attribute: NSLayoutAttributeNotAnAttribute \n                                multiplier: 0.0 \n                                  constant: _expandedHeight];\n[self.view addConstraint: _heightConstraint];\n
Run Code Online (Sandbox Code Playgroud)\n

雨燕4

\n
override func viewDidLayoutSubviews() {\n        super.viewDidLayoutSubviews()\n        let heightConstraint = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 300)\n        self.view.addConstraint(heightConstraint)\n    }\n
Run Code Online (Sandbox Code Playgroud)\n