2

I am looking for help on a way to handle the rotation notification from an iOS 8 device (iPhone 6) so I can load a different xib for the Landscape keyboard.

I have a few portrait keyboards, each of which loads from its xib file and all works for that, but I want to load another xib file for the landscape layout and have not found a solution that works.

I have tried registering for the notification as recommended in many other answers here but it hasn't worked.

I have tried all that is recommended here: How to detect Orientation Change in Custom Keyboard Extension in iOS 8?, however determining whether we are in Landscape or Portrait by detecting the size as recommended still didn't work.

Adding some code that is used:

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

I also tried using the orientation change notification

// 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;
    }
}

I have tried most of the solutions proposed on Stack Overflow but nothing worked for an iOS 8 custom Keyboard. Anyone know or seen a solution that works will be really great.

Community
  • 1
  • 1
Ohadi
  • 31
  • 3

1 Answers1

0

Don't put this code in 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;
}

Instead move it to viewDidLayoutSubviews which is called whenever the device rotates.

Stefan
  • 5,203
  • 8
  • 27
  • 51
Duc
  • 650
  • 5
  • 10