How do I detect the iOS keyboard when it stays up between controllers?

偶尔善良 提交于 2019-12-24 00:24:05

问题


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

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.


回答1:


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.

EDIT:

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

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


来源:https://stackoverflow.com/questions/11800963/how-do-i-detect-the-ios-keyboard-when-it-stays-up-between-controllers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!