更改键盘扩展中的视图高度 (iPhone X)
change views height in Keyboard extension (iPhone X)
我有以下问题:我构建了一个键盘扩展,一个替代系统键盘的键盘。在所有较旧的 iPhone 型号中,它工作正常,但在 iPhone X 中则不然。我的问题是,键盘视图的高度从 216px (iPhone 8 ,8+ , ...) 减少到 141px (iPhone X)。因为高度较低,我的键盘按钮现在更小,为了更好的用户可用性。
我确实使用 .xib 文件创建了 UI,我以编程方式添加了所有 UI - 项目。
我的问题
是否可以为键盘扩展视图(特别是 iPhone X)获得更多 space(高度)?
我解决了我的问题。我在 Apple documentation 中找到以下段落:
You can adjust the height of your custom keyboard’s primary view using Auto Layout. By default, a custom keyboard is sized to match the system keyboard, according to screen size and device orientation. A custom keyboard’s width is always set by the system to equal the current screen width. To adjust a custom keyboard’s height, change its primary view's height constraint.
The following code lines show how you might define and add such a constraint:
CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint =
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 0.0
constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];
Swift 4
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let heightConstraint = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 300)
self.view.addConstraint(heightConstraint)
}
将约束添加到 ViewDidLoad
方法可以解决问题。
override func viewDidLoad() {
super.viewDidLoad()
let heightConstraint = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 220)
self.view.addConstraint(heightConstraint)
setupKeyboard()
注意:创建子视图会产生内存警告。
我有以下问题:我构建了一个键盘扩展,一个替代系统键盘的键盘。在所有较旧的 iPhone 型号中,它工作正常,但在 iPhone X 中则不然。我的问题是,键盘视图的高度从 216px (iPhone 8 ,8+ , ...) 减少到 141px (iPhone X)。因为高度较低,我的键盘按钮现在更小,为了更好的用户可用性。
我确实使用 .xib 文件创建了 UI,我以编程方式添加了所有 UI - 项目。
我的问题
是否可以为键盘扩展视图(特别是 iPhone X)获得更多 space(高度)?
我解决了我的问题。我在 Apple documentation 中找到以下段落:
You can adjust the height of your custom keyboard’s primary view using Auto Layout. By default, a custom keyboard is sized to match the system keyboard, according to screen size and device orientation. A custom keyboard’s width is always set by the system to equal the current screen width. To adjust a custom keyboard’s height, change its primary view's height constraint.
The following code lines show how you might define and add such a constraint:
CGFloat _expandedHeight = 500;
NSLayoutConstraint *_heightConstraint =
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 0.0
constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];
Swift 4
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let heightConstraint = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 300)
self.view.addConstraint(heightConstraint)
}
将约束添加到 ViewDidLoad
方法可以解决问题。
override func viewDidLoad() {
super.viewDidLoad()
let heightConstraint = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 220)
self.view.addConstraint(heightConstraint)
setupKeyboard()
注意:创建子视图会产生内存警告。