Is there a graceful way of getting the frame (size) of whatever inputView is currently visible? By graceful, I mean not resigning, then re-activating the first responder, since that discards any kbd state data, e.g. Shift key status.
What I currently do is this:
- (CGSize)inputViewSize
{
__block CGSize result = CGSizeZero;
UIResponder *firstResponder = [self getFirstResponder];
id observer = [NSNotificationCenter.defaultCenter addObserverForName:UIKeyboardDidShowNotification object:nil queue:nil usingBlock:^(NSNotification *note)
{
result = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
}];
[firstResponder resignFirstResponder];
[firstResponder becomeFirstResponder];
[NSNotificationCenter.defaultCenter removeObserver:observer];
return result;
}
EDIT: What I should have been doing is this:
@implementation UIApplication (KeyboardFrame)
static CGRect _keyboardFrame = (CGRect){ (CGPoint){ 0.0f, 0.0f }, (CGSize) { 0.0f, 0.0f } };
+ (CGSize)keyboardSize { return _keyboardFrame.size; }
+ (void)load
{
[NSNotificationCenter.defaultCenter addObserverForName:UIKeyboardDidShowNotification object:nil queue:nil usingBlock:^(NSNotification *note)
{
_keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
}];
[NSNotificationCenter.defaultCenter addObserverForName:UIKeyboardDidChangeFrameNotification object:nil queue:nil usingBlock:^(NSNotification *note)
{
_keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
}];
[NSNotificationCenter.defaultCenter addObserverForName:UIKeyboardDidHideNotification object:nil queue:nil usingBlock:^(NSNotification *note)
{
_keyboardFrame = CGRectZero;
}];
}
@end