简体   繁体   中英

UITextView inside UIScrollView scroll problem

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM