0

現在表示されている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
4

1 に答える 1

0

キーボード通知を観察する必要があります。キーボード フレームの変更に関心がある場合は、次の 2 つの通知を利用できます。

UIKeyboardWillChangeFrameNotification 
UIKeyboardDidChangeFrameNotification 
于 2015-05-12T16:04:40.670 に答える