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

ios - 在键盘演示中点击 UITextView 或 UITextField 时出现 UIScrollView 问题

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

我有一个 UIScrollView,其中包含 UITextFieldsUITextViews。我已经注册了 UIKeyboardDidChangeFrameNotification。当我点击文本字段或 TextView 时,会触发确实更改框架通知操作,我调整 ScrollView 的 contentOffset,如下所示

- (void)keyboardDidChangeFrameNSNotification *)notification
{
    CGRect keyboardEndFrame;
    [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGRect intersection;
    UIView *theFirstResponder = [[UIApplication sharedApplication].keyWindow findFirstResponder];

    if ([theFirstResponder isKindOfClass:[UITextView class]]) {
        if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) {
            keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 416, keyboardEndFrame.size.height, keyboardEndFrame.size.width);
        }
        else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
        {
            keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, 0, keyboardEndFrame.size.height, keyboardEndFrame.size.width);
        }
    }
    else
        keyboardEndFrame = CGRectMake(keyboardEndFrame.origin.y, keyboardEndFrame.origin.x, keyboardEndFrame.size.height, keyboardEndFrame.size.width);

    screenRect = CGRectMake(screenRect.origin.y, screenRect.origin.x, screenRect.size.height, screenRect.size.width);
    if(CGRectEqualToRect(lastKBDRect, keyboardEndFrame)) {
        return;
    }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    lastKBDRect = keyboardEndFrame;
    if (CGRectIntersectsRect(keyboardEndFrame, screenRect)) {
        // Keyboard is visible

        //Convert Frame of the first responder, in context of the view that needs to be shifted
        UIView *firstResponder = [[UIApplication sharedApplication].keyWindow findFirstResponder];
        CGRect theRect = [firstResponder convertRect:firstResponder.frame toView:[UIApplication sharedApplication].keyWindow];
        theRect = CGRectMake(theRect.origin.y, theRect.origin.x > 768 ? 750 : theRect.origin.x, theRect.size.height, theRect.size.width);

        intersection = CGRectIntersection(keyboardEndFrame, theRect);

        //If intersection is null, then no need to shift anything. Simply return.
        if(CGRectIsNull(intersection)) {
            return;
        }
        //Shift the view so that the first responder view is completely visible, keeping the constraint that the origin of the first responder view is also visible.


        //Remember the current offset, so when we shift the view back, we shift it to the proper position.
        if (!wasContentViewShifted) {
            lastContentOffset = contentScrollView.contentOffset;
            lastContentSize = contentScrollView.contentSize;
            wasContentViewShifted = YES;
        }

        CGFloat offset = theRect.origin.y + theRect.size.height - keyboardEndFrame.origin.y;
        if((theRect.origin.y - offset) < 40) {
            offset += 42;
        }
        [UIView animateWithDuration:0.3f animations:^{
        contentScrollView.contentOffset = CGPointMake(0, contentScrollView.contentOffset.y + offset);
            contentScrollView.contentSize = CGSizeMake(0, lastContentSize.height + (600 - theRect.size.height));
    }];

    } else {
        // Keyboard is hidden. Move the view back only if it was shifted.
        if(wasContentViewShifted) {
            wasContentViewShifted = NO;
            [UIView animateWithDuration:0.3f animations:^{
                contentScrollView.contentOffset = lastContentOffset;
               contentScrollView.contentSize = lastContentSize;
            }];
        }
    }
}

应用程序仅支持横向。 我在这里面临的问题是

  1. 点击 textView 会显示键盘,如果 textview 被键盘隐藏,则会滚动到顶部。现在,改变方向(横向从左到横向)使 textView 滚动到顶部,因此不可见。
  2. TextView 的滚动有时适用于横向左侧,但不适用于横向右侧,反之亦然。这是因为我使用了 keyboardEndFrame 值。我不应该使用 keyboardEndFrame 原始值吗?如果没有,还有什么替代方案?
  3. 有时它适用于 textField 但不适用于 textView。



Best Answer-推荐答案


您可以尝试使用 this,而不是使用如此复杂的解决方案. 还可以找到一种更简单的解决方案 here .

此外,不建议像您那样对帧进行硬编码。引用 apple documentation了解更多详情。

关于ios - 在键盘演示中点击 UITextView 或 UITextField 时出现 UIScrollView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15757527/

回复

使用道具 举报

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

本版积分规则

关注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