Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 885
Zhu Shengqi Avatar asked Mar 17 '23 06:03

Zhu Shengqi


1 Answers

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

like image 139
Rory McKinnel Avatar answered Apr 24 '23 23:04

Rory McKinnel