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

标签 ios iphone keyboard

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

-(void)keyboardWillHide:(NSNotification*)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 setAnimationCurve:(UIViewAnimationCurve)curve];
    [UIView setAnimationDuration:duration];
    _feedbackTextView.height+=dY;
    _feedbackUserDataView.height+=dY;
    [UIView commitAnimations];
}

-(void)keyboardWillShow:(NSNotification *)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 setAnimationCurve:(UIViewAnimationCurve)curve];
    [UIView setAnimationDuration:duration];
    _feedbackTextView.height+=dY;
    _feedbackUserDataView.height+=dY;
    [UIView commitAnimations];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_feedbackTextView resignFirstResponder];
    [_feedbackUserDataView resignFirstResponder];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)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)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)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.

令我惊讶的是,谷歌中没有关于此的内容,所以我在这里发布了一个问题。抱歉格式丑陋。

最佳答案

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

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

一个修复方法是检查 scrollView 的 panGestureRecogniser 状态,如果它在 UIGestureRecognizerStateBegan 中,则避免使文本字段成为第一响应者。

-(void)scrollViewDidEndDecelerating:(UIScrollView *)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 来关闭它。如果用户正在平移,则将其关闭。

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

相关文章:

ios - cordova.file.dataDirectory 未定义

iphone - 在应用程序中查看核心数据

iphone - 为什么我的网页不能在 Iphone 浏览器中换行?

ios - 快速处理键盘重叠

ios - 当滚动到屏幕外时,UITableView 部分无法正确绘制/EXC_Bad_Access

.net - iOS中如何实现用户注册和认证?

iOS UITapGestureRecognizer 多次点击

ios - 如何与iPhone中的Retina 3.5和4兼容

keyboard - 如何从 Apple 键盘发送 Windows Media 键

iphone - resignifirstresponder 崩溃我的 ios 应用程序