如果连接了硬件键盘,则隐藏inputAccessoryView [英] Hide inputAccessoryView if hardware keyboard is connected

查看:138
本文介绍了如果连接了硬件键盘,则隐藏inputAccessoryView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此问题类似: iPad:检测外部键盘,我正在开发一款iPad应用这是使用带有自定义 inputAccessoryView 的文本字段来为虚拟键盘提供附加功能。

Similar to this question: iPad: Detecting External Keyboard, I am developing an iPad app that is using text fields with a custom inputAccessoryView to provide additional functionality for the virtual keyboard.

但是,如果是硬件键盘(例如蓝牙键盘)已连接到设备,软件键盘未按预期显示,但由于某种原因,inputAccessoryView 仍然可见位于底部屏幕。此外,这似乎导致触发 UIKeyboardDidShowNotification (因此即使硬件键盘用于输入,也会移动我的视图以避免键盘实际上不存在) 。

However, if a hardware keyboard (e.g. bluetooth keyboard) is connected to the device, the software keyboard is not shown as expected, but for some reason the inputAccessoryView is still visible at the bottom of the screen. Additionally, this seems to cause firing the UIKeyboardDidShowNotification(and therefore moving my view up to avoid occlusion by the keyboard which is actually not present) even if the hardware keyboard is used for input.

我找到了几种解决方案来检测硬件键盘是否已连接,但是所有这些都在接收到<$ c $后检查状态 c> UIKeyboardDidShowNotification ,此时inputAccessoryView已经可见(例如如何检测iPad上是否有外接键盘?)。

I found several solutions to detect if a hardware keyboard is connected, but all of them check the state after receiving a UIKeyboardDidShowNotification, at which point the inputAccessoryView is already visible (e.g. How can I detect if an external keyboard is present on an iPad?).

如果没有连接硬件键盘,我正在寻找一种只显示inputAccessoryView的方法。因此,我需要知道是否已连接硬件键盘之前 UIKeyboardDidShowNotification 已被触发。

I am looking for a way to only show a inputAccessoryView if there is no hardware keyboard connected. Therefore I need to know if a hardware keyboard is connected before a UIKeyboardDidShowNotification is fired.

此处提供的已接受解决方案如何检测iPad上是否存在外部键盘?对我来说没有选择,因为他们使用的私有API可能会导致我的应用被拒绝。

The accepted solutions provided here How can I detect if an external keyboard is present on an iPad? are no option for me as they use private APIs which may cause my app to get rejected.

推荐答案

这只是@arlomedia对答案的增强。我所做的是观察willShow和didShow。

This is just an enhancement of the answer by @arlomedia. What I did was watch for the willShow and didShow.

我使用的willShow将我的textview移动到位,使其以与键盘相同的速率移动。

The willShow I use to move my textview into position so that it moves at the same rate as the keyboard.

我使用上述技术检查键盘表观大小的didShow,并相应地隐藏/显示accessoryInputView。

The didShow I use to check the apparent size of the keyboard using the aforementioned technique and hide/show the accessoryInputView accordingly.

重要我还默认将该视图设置为隐藏,并且当收到一个willHide事件时,它会再次被隐藏。

It's important that I also set that view to be hidden by default, and that when a willHide event is received, it is hidden again then.

- (void) addKeyboardObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void) removeKeyboardObserver {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification*)notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    // If we're on iOS7 or earlier and landscape then the height is in the
    // width.
    //
    if ((IS_LANDSCAPE == YES) && (IS_IOS8_OR_LATER == NO)) {
        keyboardSize.height = keyboardSize.width;
    }

    NSNumber *rate = notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];

    CGRect textFieldFrame = self.textField.frame;
    textFieldFrame.origin.y = ([Util screenHeight] - keyboardSize.height) - textFieldFrame.size.height - [Util scaledHeight:10.0];

    // Move the text field into place.
    //
    [UIView animateWithDuration:rate.floatValue animations:^{
        self.answerTextField.frame = textFieldFrame;
    }];

    keyboardShown = YES;
}

- (void)keyboardDidShow:(NSNotification*)notification {
    CGRect keyboardBeginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGSize keyboardSize = keyboardBeginFrame.size;

    // If we're on iOS7 or earlier and landscape then the height is in the
    // width.
    //
    if ((IS_LANDSCAPE == YES) && (IS_IOS8_OR_LATER == NO)) {
        keyboardSize.height = ABS(keyboardBeginFrame.origin.x - keyboardEndFrame.origin.x); // the keyboard will move by an amount equal to its height when it appears; ABS is needed for upside-down orientations
    } else {
        keyboardSize.height = ABS(keyboardBeginFrame.origin.y - keyboardEndFrame.origin.y); // the keyboard will move by an amount equal to its height when it appears; ABS is needed for upside-down orientations
    }

    NSNumber *rate = notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];

    [UIView animateWithDuration:rate.floatValue animations:^{
        if (keyboardSize.height <= self.accessoryBar.frame.size.height) {
            self.textField.inputAccessoryView.hidden = YES;
            self.answerTextField.inputAccessoryView.userInteractionEnabled = NO;
        } else {
            self.textField.inputAccessoryView.hidden = NO;
            self.answerTextField.inputAccessoryView.userInteractionEnabled = YES;
        }
    }];

    keyboardShown = YES;
}

- (void)keyboardHidden:(NSNotification*)notification {

    NSNumber *rate = notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];

    // Remove/hide the accessory view so that next time the text field gets focus, if a hardware
    // keyboard is used, the accessory bar is not shown.
    //
    [UIView animateWithDuration:rate.floatValue animations:^{
        self.textField.inputAccessoryView.hidden = YES;
        self.answerTextField.inputAccessoryView.userInteractionEnabled = NO;
    }];

    keyboardShown = NO;
}

注意编辑添加更改为userInteractionEnabled,以便一个隐藏的accessoryView不吃水龙头。

NOTE Edited to add change to userInteractionEnabled so that a hidden accessoryView doesn't eat taps.

这篇关于如果连接了硬件键盘,则隐藏inputAccessoryView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆