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.

Zhu Shengqi
  • 3,632
  • 3
  • 24
  • 29

1 Answers1

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

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • I'm using iOS 8.3, it seems this solution doesn't work for me. Thanks anyway : ) – Zhu Shengqi Apr 29 '15 at 10:04
  • 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 http://stackoverflow.com/questions/24453108/smooth-uitextview-auto-scroll-to-bottom-of-frame-solved – Rory McKinnel Apr 29 '15 at 10:12
  • This is the same solution you mentioned above, seems not working for me : ( – Zhu Shengqi Apr 29 '15 at 11:11
  • 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. – Rory McKinnel Apr 29 '15 at 11:15
  • 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 – Zhu Shengqi Apr 29 '15 at 11:25
  • Last shot. Try setting `history.layoutManager.allowsNonContiguousLayout = NO;` Saw this here: http://stackoverflow.com/questions/26038082/uitextview-settext-should-not-jump-to-top-in-ios8 – Rory McKinnel Apr 29 '15 at 11:36
  • This works for me! Thanks a lot for your patience and brilliance : ) – Zhu Shengqi Apr 29 '15 at 11:42