4

I'm debugging a UITextView which logs my operation.

e.g. If I press a special button on the screen, this UITextView will show which button I just pressed. It will log more as I pressed more buttons, so I can easily scroll the UITextView to see my past operation.

Because the UITextView itself doesn't scroll with increasing text, so I try to make it scroll to the last line when something is logged into it. I try to use the scrollRangeToVisible() method below

history.scrollRangeToVisible(NSMakeRange(count(history.text!) - 1, 1))

Here history is a UITextView outlet.

The problem is, this method always makes the UITextView to scroll from the beginning to the designated NSRange. More specifically, it will reset the view to the beginning of the text and then scroll down to wherever the NSRange is. I want it to scroll directly down,

Can someone solve this problem? Thanks a lot.

1 Answer 1

7

This link (what to use instead of scrollRangeToVisible in iOS7 or TextKit) suggests turning scrolling off, do the change then switch it on.

history.scrollEnabled = NO;
[history scrollRangeToVisible:NSMakeRange(history.text.length - 1,0)];
history.scrollEnabled = YES;

EDIT

Solved after discussion using:

history.layoutManager.allowsNonContiguousLayout = NO; 

Saw this here: UITextView setText should not jump to top in ios8

Sign up to request clarification or add additional context in comments.

7 Comments

I'm using iOS 8.3, it seems this solution doesn't work for me. Thanks anyway : )
Another option is to disable the scroll before you add the new text, then enable it after. Perhaps the insert of text is resetting something scroll related causing it to warp to the top first. See stackoverflow.com/questions/24453108/…
This is the same solution you mentioned above, seems not working for me : (
Not the same. The first was doing the scroll request while scroll is off. The second was to do your text insertion/append while the scroll is off.
Sorry I didn't read carefully. With the second solution, my UITextView is still disabled (can't scroll) even after scrollEnabled is set to true. This is just a mystery
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.