gpt4 book ai didi

ios - 处理旋转时更改 iOS 键盘布局

转载 作者:行者123 更新时间:2023-11-29 10:34:25 25 4
gpt4 key购买 nike

根据设备的方向,我的键盘有两种略有不同的布局。当我第一次加载键盘时,它看起来不错。这是纵向版本:

Portrait Keyboard

初始约束设置发生在 -viewDidLoad(属于 UIInputViewController 子类)

portraitConstraints = [self constraintsForOrientation:UIInterfaceOrientationPortrait];
landscapeConstraints = [self constraintsForOrientation:UIInterfaceOrientationLandscapeRight];

[self.view addConstraints:portraitConstraints];
[self.view addConstraints:landscapeConstraints];

if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
[NSLayoutConstraint deactivateConstraints:landscapeConstraints];
} else {
[NSLayoutConstraint deactivateConstraints:portraitConstraints];
}

这部分工作正常,因为无论初始方向如何,它都能正确加载。但是,一旦我旋转设备,一切都出错了。

Wrong

然后回到纵向:

Wrong

作为引用,正确的横向版本如下所示:

Landscape

我更新了 -updateViewConstraints 中的约束,如下所示:

- (void)updateViewConstraints {
if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
[NSLayoutConstraint deactivateConstraints:landscapeConstraints];
[NSLayoutConstraint activateConstraints:portraitConstraints];
} else {
[NSLayoutConstraint deactivateConstraints:portraitConstraints];
[NSLayoutConstraint activateConstraints:landscapeConstraints];
}

[super updateViewConstraints];
}

似乎在 View 更改后,它突然占据了比全屏更多的空间,而不仅仅是键盘区域。有什么办法可以解决这个问题吗?谢谢。

最佳答案

这个问题似乎与以某种方式隐式覆盖包含键盘的 View 的高度有关。虽然我没有看到任何会强制调整容器 View 大小的内容,但显式设置容器 View 的高度得到了良好的、可重现的结果。

我想到了从 Apple's App Extension Programming Guide: Custom Keyboard 手动限制键盘高度的想法.

具体来说,相关信息是这样的:

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];

NOTE

In iOS 8.0, you can adjust a custom keyboard’s height any time after its primary view initially draws on screen.

以下是基于此信息的模板答案中的重要方法:

- (void)updateViewConstraints {
if (self.keyboardHeightConstraint == nil) {
// Just starting with SOME value for the height
self.keyboardHeightConstraint =
[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0f
constant:500.0f];
[self.view addConstraint:self.keyboardHeightConstraint];
}
// Obviously, these values will be changed based on device AND orientation
// These are bogus values...
if ([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height) {
self.keyboardHeightConstraint.constant = 300.0f;
[NSLayoutConstraint deactivateConstraints:self.landscapeConstraints];
[NSLayoutConstraint activateConstraints:self.portraitConstraints];
} else {
self.keyboardHeightConstraint.constant = 400.0f;
[NSLayoutConstraint deactivateConstraints:self.portraitConstraints];
[NSLayoutConstraint activateConstraints:self.landscapeConstraints];
}
[super updateViewConstraints];
}

关于ios - 处理旋转时更改 iOS 键盘布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27838749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com