現在表示されているinputViewのフレーム(サイズ)を取得する適切な方法はありますか? グレースフルとは、辞任せずにファーストレスポンダーを再アクティブ化することを意味します。これは、Shift キーのステータスなど、kbd の状態データを破棄するためです。
私が現在していることはこれです:
- (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;
}
編集:私がすべきだったのはこれです:
@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