Zhu Shengqi
Zhu Shengqi

Reputation: 3792

UITextView always scrolls from the beginning when using scrollRangeToVisible()

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.

Upvotes: 4

Views: 2235

Answers (1)

Rory McKinnel
Rory McKinnel

Reputation: 8014

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

Upvotes: 7

Related Questions