• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 有键盘会显示通知,但键盘窗口为零

[复制链接]
菜鸟教程小白 发表于 2022-12-13 02:51:44 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有点困惑。我正在创建一个应用程序,用户可以在页面之间水平滑动 ScrollView ,并且每个页面都有一个文本字段或一个 TextView 。我订阅了 UIKeyboardWillShowNotification 和 UIKeyboardWillHideNotification 通知,并且卡片在键盘弹出时会降低高度,在键盘隐藏时会增加回来。 此外,当拖动 ScrollView 时,所有 textViews/textField 都会辞职第一响应者。就是这个样子:

-(void)keyboardWillHideNSNotification*)notification{
//    NSLog(@"notification user info = %@",notification.userInfo);
    auto userInfo=notification.userInfo;
    auto curve=[userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
    auto duration=[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    auto startFrame=[userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    auto endFrame=[userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    auto dY=endFrame.origin.y-startFrame.origin.y;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurveUIViewAnimationCurve)curve];
    [UIView setAnimationDuration:duration];
    _feedbackTextView.height+=dY;
    _feedbackUserDataView.height+=dY;
    [UIView commitAnimations];
}

-(void)keyboardWillShowNSNotification *)notification{
//    NSLog(@"notification user info = %@",notification.userInfo);
    auto userInfo=notification.userInfo;
    auto curve=[userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
    auto duration=[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    auto startFrame=[userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    auto endFrame=[userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    auto dY=endFrame.origin.y-startFrame.origin.y;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurveUIViewAnimationCurve)curve];
    [UIView setAnimationDuration:duration];
    _feedbackTextView.height+=dY;
    _feedbackUserDataView.height+=dY;
    [UIView commitAnimations];
}

-(void)scrollViewDidScrollUIScrollView *)scrollView{
    [_feedbackTextView resignFirstResponder];
    [_feedbackUserDataView resignFirstResponder];
}

-(void)scrollViewDidEndDeceleratingUIScrollView *)scrollView{
//    NSLog(@"%s",sel_getName(_cmd));
    CGFloat width = scrollView.frame.size.width;
    NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;
    switch(page){
        case 0:{
            [_feedbackTextView becomeFirstResponder];
        }break;
        case 1:{
            [_feedbackUserDataView becomeFirstResponder];
        }break;
    }
}

-(void)scrollViewDidEndDraggingUIScrollView *)scrollView willDecelerateBOOL)decelerate{
//    NSLog(@"decelerate = %d",decelerate);
    if(decelerate==NO){
        CGFloat width = scrollView.frame.size.width;
        NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;
        switch(page){
            case 0:{
                [_feedbackTextView becomeFirstResponder];
            }break;
            case 1:{
                [_feedbackUserDataView becomeFirstResponder];
            }break;
        }
    }
}

_feedbackTextView是scrollView第一页的子view,_feedbackUserDataView是scrollView第二页的子view。 我测试了我的应用程序。我滚动,困惑地点击 textViews/textFields。我在日志中收到警告

|warning| Got a keyboard will show notification, but keyboard window is nil.

令我惊讶的是,谷歌对此一无所知,所以我在这里发布了一个问题。很抱歉格式不好。



Best Answer-推荐答案


此警告似乎是在键盘呈现后立即关闭时产生的,在动画完成之前。

在这种情况下 scrollViewDidEndDecelerating: 可以在用户再次开始平移而 ScrollView 仍在减速时被调用。然后立即调用 scrollViewDidScroll:,这导致键盘出现并立即关闭。

一个解决方法是检查 scrollView 的 panGestureRecogniser 状态,如果它在 UIGestureRecognizerStateBegan

中,则避免使文本字段成为第一响应者
-(void)scrollViewDidEndDeceleratingUIScrollView *)scrollView{

    if ([scrollView panGestureRecognizer].state == UIGestureRecognizerStateBegan) {
        return;
    }

    //    NSLog(@"%s",sel_getName(_cmd));
    CGFloat width = scrollView.frame.size.width;
    NSInteger page = (scrollView.contentOffset.x + (0.5f * width)) / width;
    switch(page){
        case 0:{
            [_feedbackTextView becomeFirstResponder];
        }break;
        case 1:{
            [_feedbackUserDataView becomeFirstResponder];
        }break;
    }
}

这应该会删除大部分警告。如果用户在键盘出现时开始平移,可能仍然存在一些问题。这些将更难处理 - 您需要确保键盘已完成其演示动画,然后再通过订阅 UIKeyboardDidShowNotification 并在用户平移时将其关闭。 p>

关于ios - 有键盘会显示通知,但键盘窗口为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26582656/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap