2

ビューベースのアプリケーションがあり、サブビューの1つにUIScrollViewがあります。キーボードが表示されたり消えたりしたときにスクロールビューのサイズを調整するハンドラーを作成しました。ユーザーがビューを離れるときにキーボードを閉じたいので、を呼び出し[currentField resignFirstResponder]ますviewWillDisappear。これによりキーボードが閉じられますが、ハンドラーを呼び出してスクロールビューのサイズを変更することはありません(他の場所で同じコードを呼び出すと、呼び出されます)。助言がありますか?

編集:これらは私が使用するハンドラーです:

-(void) keyboardWasShown:(NSNotification*) notification
{
    if(keyboardShown)
        return;

    NSDictionary* info=[notification userInfo];
    NSValue* value=[info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize=[value CGRectValue].size;
    CGRect viewFrame=[scrollView frame];
    viewFrame.size.height-=keyboardSize.height;
    scrollView.frame=viewFrame;

    keyboardShown=YES;
}

-(void) keyboardWasHidden:(NSNotification*) notification
{
    NSDictionary* info=[notification userInfo];
    NSValue* value=[info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize=[value CGRectValue].size;
    CGRect viewFrame=[scrollView frame];
    viewFrame.size.height+=keyboardSize.height;
    scrollView.frame=viewFrame;

    keyboardShown=NO;
}

他の場所を呼び出す[currentField resignFirstResponder]と、問題なくハンドラーが呼び出されます。

4

1 に答える 1

0

So you were being removed as observer before UIKeyboardDidHideNotification was posted, glad I could help. But observing the UIKeyboardWillHideNotification and UIKeyboardWillShowNotification is probably enough for your reaction to the keyboard. The keyboard notifications have a user info key UIKeyboardAnimationDurationUserInfoKey which you can use to animate your frame adjustments with the keyboard animations. This avoids the 'clunk' feeling your views will have if you don't animate them to new positions. Here is a quick example of what you can do:

-(void)keyboardWillNotificationTarget:(NSNotification *)note{
        // Find current keyboard origin Y 
    NSValue *keyboardCurrentFrameValue = [note.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGFloat currentOriginY = keyboardCurrentFrameValue.CGRectValue.origin.y;
        // Find keyboard Y that will be
    NSValue *keyboardNewFrameValue = [note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat newOriginY = keyboardNewFrameValue.CGRectValue.origin.y;
        // Calculate new frame for scrollView
    CGFloat heightChangeForScrollView = newOriginY - currentOriginY;
    CGRect svFrame = scrollView.frame;
    svFrame.size.height += heightChangeForScrollView;
        // Find duration of animation
    NSNumber *animationDurationNumber = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    CGFloat animationDuration = animationDurationNumber.floatValue;
        // Animate scrollView with keyboard
    [UIView animateWithDuration:animationDuration animations:^{
        scrollView.frame = svFrame;
    }];
}

Now you simply add this method as the target for both notifications:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillNotificationTarget:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillNotificationTarget:) name:UIKeyboardWillHideNotification object:nil];
于 2012-01-26T13:20:04.040 に答える