2

我有一个基于视图的应用程序,其中一个子视图中有一个 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 回答