如果连接了硬件键盘,则隐藏 inputAccessoryView

2023-12-30

类似于这个问题:iPad:检测外接键盘 https://stackoverflow.com/questions/5019471/ipad-detecting-external-keyboard,我正在开发一个 iPad 应用程序,该应用程序使用文本字段和自定义inputAccessoryView为虚拟键盘提供附加功能。

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

我找到了几种检测硬件键盘是否已连接的解决方案,但它们都检查状态after收到一个UIKeyboardDidShowNotification,此时 inputAccessoryView 已经可见(例如如何检测 iPad 上是否存在外接键盘? https://stackoverflow.com/questions/2893267/how-can-i-detect-if-an-external-keyboard-is-present-on-an-ipad).

我正在寻找一种仅在没有连接硬件键盘的情况下显示 inputAccessoryView 的方法。因此我需要知道是否连接了硬件键盘before a UIKeyboardDidShowNotification被解雇了。

此处提供的已接受的解决方案如何检测 iPad 上是否存在外接键盘? https://stackoverflow.com/questions/2893267/how-can-i-detect-if-an-external-keyboard-is-present-on-an-ipad对我来说没有选择,因为它们使用私有 API,这可能会导致我的应用程序被拒绝。


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

我用 willShow 将文本视图移动到位,以便它以与键盘相同的速度移动。

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

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

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

NOTE编辑以添加对 userInteractionEnabled 的更改,以便隐藏的accessoryView不会吃水龙头。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果连接了硬件键盘,则隐藏 inputAccessoryView 的相关文章

随机推荐

  • Dozer 映射类级别是可访问的

    我正在使用推土机框架来克隆我的对象 我希望推土机框架在不使用 getter 和 setter 的情况下克隆数据 为此我在类级别设置 is accessible 属性 但这似乎不起作用 当我在字段级别设置 is accessible 时 它
  • 使用 Windows mklink 链接 2 个文件

    我试图在 Windows 上找到 nix 符号链接的等效项 并开始使用 mklink 问题是 作为普通用户 不是管理员 我可以使用 J 选项链接到文件夹 但无法链接到文件 我设法以管理员身份执行此操作 但我需要以标准用户身份执行此操作 为什
  • 为什么使用 Scala-IDE 的 Scala 工作表会给出此错误?

    在 Scala 工作表中我可以这样做 object Play println Playing a bit gt Playing a bit case class X a Int 1 x List X 但我不能这样做 object Play
  • JWT 实际上如何与 Spring MVC 一起创建令牌并验证令牌?

    我实际上希望获得更多 更清晰的理解JWT概念及其运作方式Spring MVC 我找到了链接https github com nielsutrecht jwt angular spring https github com nielsutre
  • REACT 应用程序调用安全的 Azure WEBAPI 服务 - 无用户

    我创建了一个简单的 REACT 应用程序 该应用程序仅在连接到我们网络上大屏幕的本地 PC 上运行 仅供内部使用 它就像广告牌或仪表板 零用户交互 屏幕不是触摸屏 也没有连接键盘和鼠标 因此没有用户可以登录 构建 REACT 应用程序 然后
  • Codeigniter flash 数据在某些浏览器中不起作用

    我在使用 Codeigniter flashdata 时遇到一些问题 在 Firefox 中似乎一切正常 但在 Chrome 以及我的移动 BB 浏览器 FWIW 中 页面之间似乎没有持久保存 flashdata 我还应该注意到 当我在本地
  • openssl 握手失败

    我正在尝试编写简单的 C openssl 客户端和服务器 这是客户的代码 int main int err SSL CTX ctx init ctx client cert pem client private pem certs cace
  • 当托管在 WinForms 容器中时,WPF ScrollViewer 不会接收鼠标事件

    我们有一个 WinForms 应用程序 我们正在逐步将其转换为 WPF 此时 应用程序的主窗体是一个 Form WinForms 其中包含 WPF 中内置的垂直侧边栏 侧边栏托管在 ElementHost 控件中 侧边栏由包含其他控件的 S
  • Sql查询性能慢

    我正在编写一个 SQL 查询 这给我带来了缓慢的性能 因此 它给我带来了 504 网关超时问题 请帮助我重新创建此查询 以便我的输出结果更快 我将把查询放在下面 select r c1 parent item c2 parent item
  • 类型“NSNotification.Name”没有成员“UITextField”

    在 Swift 4 2 中 出现以下错误 在 Swift 4 中工作正常 类型 NSNotification Name 没有成员 UITextField 这是我的错误代码 NotificationCenter default addObse
  • 以引用对象为标准的 mongoid 作用域

    我在 Rails 3 中的 Mongoid 模型有以下范围 class Expert include Mongoid Document referenced in category scope currently available lam
  • 在不违反 REST 的情况下处理长查询

    我们有一个 REST api 并且我们在坚持 REST 精神方面做得非常好 然而 我们有一个重要的消费者 他们请求一种方法来协调他们的数据存储 流程如下 消费者进行 GET 调用来检索在某个日期范围内创建的所有库存对象 假设这会返回 100
  • Antlr 数组帮助

    嘿 我开始在 java 中使用 Antlr 我想知道如何将一些值直接存储到二维数组中并返回该数组 我根本找不到任何关于此的教程 感谢所有帮助 假设您想要解析一个包含由空格分隔的数字的平面文本文件 您想将其解析为二维数组int其中每一行都是数
  • 为什么动态构造对于 php 编译器 (PHP) 来说很困难?

    我正在读保罗 比格的书http blog paulbiggar com archive a rant about php compilers in general and hiphop in pspecial http blog paulb
  • 无法使用集成在 P4 中创建新分支

    我在 P4 有一个分行 depot MyDemoInfo trunk Server My Service 在 My Service 下 存在我的整个源代码 现在 当我尝试从上面的主干分支创建一个新分支时 它给了我错误 我正在尝试创建一个新的
  • ggplot geom_tile 与面的间距

    我正在尝试制作一个按 x 轴上的两个离散变量排序的多面 ggplot 问题是我想让垂直相邻的条目全部接触 目前 行之间存在空间 具体取决于顶部图与底部图中因子的水平 抱歉 这个可重现的示例有点冗长 npats 20 simsympt c i
  • 如何将反应式rhandsontable重置为默认值?

    我正在构建一个应用程序 其中 2 2 表包含一些用于进一步计算的值 这些值可以由用户更新 并且用户将能够恢复到原始值 我试图通过一个操作按钮来实现它 该按钮会将表重置为其原始值 但表不会更新 这是一个简化的示例 rm list ls lib
  • HTML 电子邮件中的内联边框样式

    我正在处理响应式 HTML 电子邮件 并且仅在 IE 中的 Gmail 中遇到渲染问题 必须如此 它在其他 27 个客户端变体中运行良好 我们需要支持 我在这里设置了一个小提琴 http jsfiddle net 39gzj http js
  • 搭载 iOS 5 和 iOS 6 的 Facebook

    我即将发布一个应用程序 它必须同时支持 iOS 5 和 iOS 6 但是对于新的 Facebook SDK 3 1 我不太确定如何集成 Facebook 功能以与两个 iOS 版本一起使用 让登录和墙贴操作在 iOS5 和 iOS6 版本中
  • 如果连接了硬件键盘,则隐藏 inputAccessoryView

    类似于这个问题 iPad 检测外接键盘 https stackoverflow com questions 5019471 ipad detecting external keyboard 我正在开发一个 iPad 应用程序 该应用程序使用