jjzjj

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

coder 2024-01-26 原文

我有一个 UIScrollView,它包含 UITextFieldsUITextViews。我已经注册了 UIKeyboardDidChangeFrameNotification。当我点击文本字段或 TextView 时,会触发 did change 帧通知操作,我调整 ScrollView 的 contentOffset 如下所示

- (void)keyboardDidChangeFrame:(NSNotification *)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。

最佳答案

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

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

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

有关ios - 在键盘演示中点击 UITextView 或 UITextField 时出现 UIScrollView 问题的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  4. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  5. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

  6. ruby - 在 Ruby 中用键盘诅咒数组浏览 - 2

    我正在尝试在Ruby中制作一个cli应用程序,它接受一个给定的数组,然后将其显示为一个列表,我可以使用箭头键浏览它。我觉得我已经在Ruby中看到一个库已经这样做了,但我记不起它的名字了。我正在尝试对soundcloud2000中的代码进行逆向工程做类似的事情,但他的代码与SoundcloudAPI的使用紧密耦合。我知道cursesgem,我正在考虑更抽象的东西。广告有没有人见过可以做到这一点的库或一些概念证明的Ruby代码可以做到这一点? 最佳答案 我不知道这是否是您正在寻找的,但也许您可以使用我的想法。由于我没有关于您要完成的工作

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

随机推荐