【问题标题】:IOS 8 custom keyboard rotation handling to load another xib file for the landscape keyboardIOS 8 自定义键盘旋转处理为横向键盘加载另一个 xib 文件
【发布时间】:2015-05-11 02:10:49
【问题描述】:

我正在寻求有关如何处理来自 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 自定义键盘没有任何效果。任何知道或见过可行的解决方案的人都会非常棒。

【问题讨论】:

  • 你的问题解决了吗?

标签: ios landscape-portrait custom-keyboard ios8-extension


【解决方案1】:

不要把这段代码放在 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

【讨论】:

  • 这个方法在改变方向的时候被多次调用!
猜你喜欢
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多