问题
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