scrollRangeToVisible 滚动方式与应用内滚动到顶部不同

发布于 2024-10-21 18:18:33 字数 454 浏览 2 评论 0原文

用户完成编辑后,我尝试滚动到 UITextView 的开头。这工作正常:

[textView scrollRangeToVisible:NSMakeRange(0, 0)];

但是,如果用户滚动到最顶部,我会得到不同的结果;如果用户滚动到顶部,UITextView 有一个很好的填充。如果我以编程方式执行此操作,则不会。我附上了一张屏幕截图来澄清这一点。

我如何以编程方式实现相同的目标,即如果用户滚动到应用程序顶部,不会有什么不同?

我需要对scrollRectToVisible 做一些事情吗? [textView scrollRectToVisible:CGRectMake(0, 0, 0, 0) 动画:是];但没用...但也许我发送了错误的参数。

在此处输入图像描述

I am trying to scroll to the beginning of my UITextView after the user is done editing. This works fine:

[textView scrollRangeToVisible:NSMakeRange(0, 0)];

However, I get a different result if the user scrolls to the very top; if the user scrolls to the top, the UITextView has a nice padding. If I do it programatically, it doesn't. I attached a screenshot to clarify this.

How would I achieve the same thing programatically, i.e. that there won't be a difference if the user scrolls to the top of the app?

Would I need to do something with scrollRectToVisible? [textView scrollRectToVisible:CGRectMake(0, 0, 0, 0) animated:YES]; did not work, though... but perhaps I'm sending the wrong parameters.

enter image description here

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

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

发布评论

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

评论(2

烂人 2024-10-28 18:18:33

免责声明:这似乎已停止与 iOS 14 一起使用。

这可能很愚蠢,但您是否尝试过使用负 y 值,例如

[textView scrollRectToVisible:CGRectMake(0, -1, 0, 0) animated:YES];

?我不知道这是否有效。另外,我不确定它是否适用于零宽度和大小的矩形,所以我宁愿使用

[textView scrollRectToVisible:CGRectMake(0, -1, 1, 1) animated:YES];

Disclaimer: This seems to have stopped working with iOS 14.

This may be stupid, but have you tried using a negative y value, like

[textView scrollRectToVisible:CGRectMake(0, -1, 0, 0) animated:YES];

? I have no if this works though. Also, I'm not sure if it works properly for a rect of zero width and size, so I'd rather use

[textView scrollRectToVisible:CGRectMake(0, -1, 1, 1) animated:YES];
顾挽 2024-10-28 18:18:33

您必须使用以下内容来获取滚动操作中包含的顶部填充:

[yourTextView setContentOffset:CGPointZero animated:YES];

由于某些奇怪的原因,使用scrollToRect不起作用,即使仍然在iOS14中,也只能指向。

You have to use the following to get the top padding included in the scroll action:

[yourTextView setContentOffset:CGPointZero animated:YES];

Using scrollToRect doesn't work for some odd reason, even still in iOS14, only to point.

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