UITextView里面UIScrollView滚动问题

发布于 2024-09-11 02:41:31 字数 600 浏览 1 评论 0原文

我遇到了一些在我的情况下被认为是错误的事情。也许这不是一个错误,而是一个功能;)。

这是我的情况:

我用我的内容加载 UIScrollView 。我的部分内容是在视图加载后异步加载的。这效果很好并且没有问题。 然而,其中一些控件是 UITextView 控件。一旦我在 viewDidLoad 之后设置了这些控件的内容(文本属性),我的 UIScrollView 就会向下滚动到最后设置的 UITextView。我想防止这种情况发生,并希望 UIScrollView 保持其顶部滚动位置。

总之: 如果我在 viewDidLoad 方法中设置 Text 属性,则不会发生滚动。如果我在 viewDidLoad 方法之后设置 UITextView 上的 Text 属性(因为我异步加载数据),UIScrollView 将滚动到设置的最后一个 UITextView。我想阻止这个滚动。

我该如何实现这一目标?我有一种感觉,这只是一个设置错误的属性,但我似乎找不到它。

提前致谢。

编辑:

我尝试在设置值之前将“scrollEnabled”属性设置为“否”,然后设置为“是”,但这没有任何效果。当设置 UITextView 的 text 属性时,ScrollView 仍然会滚动。

I'm experiencing something considered a bug in my situation. Probably this is not a bug but a feature ;).

Here's my case:

I load a UIScrollView with my content. A part of my content is loaded asynchrone after the view is already loaded. This works great and without issue.
Some of these controls however are UITextView controls. Once I set the content (text property) of these controls after the viewDidLoad my UIScrollView scrolls down to the last UITextView that was set. I want to prevent this and want the UIScrollView to maintain it's top scrolled position.

In summary:
If I set the Text property in the viewDidLoad method no scroll occurs. If I set the Text property on the UITextView after the viewDidLoad method (because I load the data asynchronous) the UIScrollView will scroll to the last UITextView that was set. I want to prevent this scroll.

How do I achieve this ? I have a feeling this is just a property which is set wrong but I can't seem to find it.

Thanks in advance.

EDIT:

I've tried setting the "scrollEnabled" property to NO before setting the values and to YES after but this didn't have any effect. The ScrollView will still scroll when the text property of the UITextView is set.

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

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

发布评论

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

评论(3

银河中√捞星星 2024-09-18 02:41:31

我通过解决方法解决了这个问题:

我将滚动视图内容大小设置为较小的值,例如 320 x 300 或滚动视图框架的高度,然后完成我的工作,然后将内容大小恢复正常。

这可以防止在加载数据时滚动,并在加载完成后立即启用滚动。

I Fixed the issue with a work-around:

I set the scroll view content size to something small, like 320 x 300 or whatever the height of the scrollview frame is, then did my work, then put the content size back to normal.

This prevents the scrolling while the data is loaded and enables it as soon as the loading is finished.

怎言笑 2024-09-18 02:41:31

这不会改变滚动问题,但可能会使其对用户“隐藏”:

yourTextView.text = @"Eat more bananas!";
yourScrollView.contentOffset = CGPointMake(0.0, 0.0);

代码的一些片段将有助于更具体地面对问题。

编辑:
或者尝试将 UITextViews 添加到 UIView,然后将 UIView 添加到 UIScrollView。确保 UIScrollView 的 contentSize 与 UIView 的 Size 相同。

This would not change the scrolling problem but maybe make it "hidden" for the user:

yourTextView.text = @"Eat more bananas!";
yourScrollView.contentOffset = CGPointMake(0.0, 0.0);

Some snippets of your code would help to face the problem more specific.

EDIT:
Or try to add the UITextViews to a UIView, then add the UIView to the UIScrollView. Make sure that the UIScrollView's contentSize is the same as the Size of the UIView.

甜心小果奶 2024-09-18 02:41:31

这对我也有用:p 但我必须将高度设置为小于 300(在我的例子中我只使用了 10)。

基本上,这个想法是在更改 UITextView 的文本时使文本视图不成为 UIScrollView 可见区域的一部分。

This worked for me too :p but i had to set the height to something less then 300 (in my case i just used 10).

Basically the idea is to make the text view not part of the visible area of the UIScrollView wile you are changing the text of th UITextView.

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