当iOS键盘停留在控制器之间时,我该如何检测? [英] How do I detect the iOS keyboard when it stays up between controllers?

查看:85
本文介绍了当iOS键盘停留在控制器之间时,我该如何检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅供初学者使用:我已经在听键盘会出现/消失/更改通知.他们没有开火.都没有出现/消失/改变.

Just for starters: I'm already listening to keyboard will appear/disappear/change notifications. They're not firing. Neither are did appear/disappear/change.

当我抬起键盘,并在上面也抬起键盘的控制器上推(viewWillAppear中的-[UITextView成为FirstResponder])时,不会触发任何键盘通知.这是有道理的,因为在此动画中键盘实际上没有移动,但是在这种情况下当然不是理想的.

When I have the keyboard up, and push a controller on top which also has the keyboard up (-[UITextView becomeFirstResponder] in viewWillAppear), no keyboard notifications are fired. This makes some sense, as the keyboard does not actually move in this animation, but it's certainly not desirable in this case.

在没有发出通知的情况下,我将如何检测这种情况,和/或如何获得键盘的当前位置?全局共享侦听器是一个选项,但我希望尽可能避免这种情况.

How would I detect this scenario, and / or how can I get the current position of the keyboard when no notification has been fired? A global, shared listener is an option, but I'd prefer to avoid that if possible.

推荐答案

您需要找到firstResponder,并且如果它是UITextField或UITextView,则需要抬起或移动键盘.没有通知意味着它已经启动,因此它的旧框架(相对于窗口)仍然有效.不幸的是,没有简单的方法来找到firstReponder.我获取了一些递归地遍历当前视图的所有子视图的代码,寻找它.

You would need to find the firstResponder, and if its a UITextField or UITextView then the keyboard is up or moving. No notification means its up already, so its old frame (relative to the window) is still valid. Unfortunately there is no easy way to find the firstReponder. I grabbed some code that recursively walked all the current view's subviews, looking for it.

- (UIView *)findFirstResponder
{
    if (self.isFirstResponder) {
        return self;     
    }

    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView findFirstResponder];
        if (firstResponder) return firstResponder;
    }
    return nil;
}

这篇关于当iOS键盘停留在控制器之间时,我该如何检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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