gpt4 book ai didi

ios - iOS 8自定义键盘旋转处理为横向键盘加载另一个Xib文件

转载 作者:行者123 更新时间:2023-12-01 16:35:09 25 4
gpt4 key购买 nike

我正在寻找有关处理来自iOS 8设备(iPhone 6)的旋转通知的方法的帮助,以便为风景键盘加载其他xib。

我有几个纵向键盘,每个键盘都可以从其xib文件加载,并且都可以使用该键盘,但是我想为横向布局加载另一个xib文件,但是找不到有效的解决方案。

我已尝试按照此处许多其他答案中的建议注册通知,但没有成功。

我在这里尝试了所有推荐的方法:How to detect Orientation Change in Custom Keyboard Extension in iOS 8?,但是通过检测建议的尺寸来确定我们是横向还是纵向仍然无法正常工作。

添加一些使用的代码:

const int keyboardHeight = 375;
const int landscapekeyboardHeight = 200;


- (void)updateViewConstraints {
[super updateViewConstraints];

NSLayoutConstraint *_heightConstraintforLandscape
= [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:landscapekeyboardHeight];

NSLayoutConstraint *_heightConstraintforPortrait
= [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:keyboardHeight];

if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height)
{
// Portrait
[self.view removeConstraint: _heightConstraintforLandscape];
[self.view addConstraint: _heightConstraintforPortrait];
}
else
{
// Landscape
[self.view removeConstraint: _heightConstraintforPortrait];
[self.view addConstraint: _heightConstraintforLandscape];
self.inputView = (UIInputView*)self.LandscapeKeyboard;
}
}

我也尝试使用方向更改通知
// Request to turn on accelerometer and begin receiving accelerometer events
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleOrientationChangeWithNotification:)
name:UIDeviceOrientationDidChangeNotification
object:nil];

- (void)handleOrientationChangeWithNotification:(NSNotification *)notification {
// Respond to changes in device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait)
{
self.inputView = (UIInputView*)self.Keyboard;
}
else
{
self.inputView = (UIInputView*)self.LandscapeKeyboard;
}
}

我已经尝试了Stack Overflow上提出的大多数解决方案,但是对于iOS 8自定义键盘没有任何作用。任何人都知道或看到可行的解决方案会非常棒。

最佳答案

不要将此代码放在updateViewConstraints中

if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height)
{
// Portrait
[self.view removeConstraint: _heightConstraintforLandscape];
[self.view addConstraint: _heightConstraintforPortrait];
}
else
{
// Landscape
[self.view removeConstraint: _heightConstraintforPortrait];
[self.view addConstraint: _heightConstraintforLandscape];
self.inputView = (UIInputView*)self.LandscapeKeyboard;
}

而是将其移动到viewDidLayoutSubviews,每当设备旋转时都会调用它。

关于ios - iOS 8自定义键盘旋转处理为横向键盘加载另一个Xib文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28950165/

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