В iOS 8 настраиваемая обработка поворота клавиатуры для загрузки другого xib-файла для альбомной клавиатуры.

Мне нужна помощь в том, как обрабатывать уведомление о вращении с устройства iOS 8 (iPhone 6), чтобы я мог загрузить другой xib для ландшафтной клавиатуры.

У меня есть несколько портретных клавиатур, каждая из которых загружается из своего файла xib, и все для этого работает, но я хочу загрузить еще один файл xib для ландшафтного макета и не нашел решения, которое работает.

Я попытался зарегистрироваться для получения уведомления, как рекомендовано во многих других ответах здесь, но это не сработало.

Я пробовал все, что рекомендуется здесь: Как определить изменение ориентации в пользовательском расширении клавиатуры в 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. Любой, кто знает или видел работающее решение, будет действительно отличным.


person Ohadi    schedule 09.03.2015    source источник
comment
Вы решили свою проблему?   -  person StrawHara    schedule 07.10.2015


Ответы (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, который вызывается при каждом повороте устройства.

person Duc    schedule 10.04.2015
comment
этот метод вызывается несколько раз при изменении ориентации! - person MhammedMohie; 18.12.2015