iOS 在视图更改时关闭键盘

发布于 2024-12-29 04:54:51 字数 1157 浏览 2 评论 0原文

我有一个基于视图的应用程序,在其中一个子视图中有一个 UIScrollView。我编写了处理程序来在键盘出现和消失时调整滚动视图的大小。我希望在用户离开视图时关闭键盘,因此我在 viewWillDisappear 中调用 [currentField resignFirstResponder]。这会关闭键盘,但不会调用处理程序来调整滚动视图的大小(当我在其他地方调用相同的代码时,它会这样做)。有什么建议吗?

编辑:这些是我使用的处理程序:

-(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] 时,它会毫无问题地调用处理程序。

I have a view-based application, and in one of the subviews there is a UIScrollView. I have written handlers to adjust the size of the scroll view when the keyboard appears and disappears. I would like the keyboard to be dismissed when the user leaves the view, so I call [currentField resignFirstResponder] in viewWillDisappear. This dismisses the keyboard, but does not call the handler to resize the scroll view (when I call the same code in other places, it does). Any suggestions?

EDIT: These are the handlers that I use:

-(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;
}

When I call [currentField resignFirstResponder] anywhere else, it calls the handler without problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2025-01-05 04:54:51

因此,在发布 UIKeyboardDidHideNotification 之前,您已被删除为观察者,很高兴我能提供帮助。但是观察 UIKeyboardWillHideNotificationUIKeyboardWillShowNotification 可能足以了解您对键盘的反应。键盘通知有一个用户信息键 UIKeyboardAnimationDurationUserInfoKey,您可以使用它通过键盘动画对帧调整进行动画处理。如果您不将视图设置为新位置,这可以避免您的视图产生“沉闷”的感觉。以下是您可以执行的操作的简单示例:

-(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;
    }];
}

现在您只需添加此方法作为两个通知的目标:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillNotificationTarget:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillNotificationTarget:) name:UIKeyboardWillHideNotification object:nil];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文