I have an NSTextView inside an NSScrollView. I put the text in an NSTextView and scroll it from the bottom programmatically, which works fine, but the scroll bar is at the top. Using the mouse to position the scroll bar leads to its transition to the lower part where it belongs, and from this point it works fine.
My code is:
textView.string = s; [textView scrollToEndOfDocument:self];
Do not suspend the scrollToEndOfDocument method - I also tried:
[textView scrollRangeToVisible:NSMakeRange(s.length, 0)]
and
[[scrollViewText contentView] scrollToPoint:NSMakePoint(0, textView.frame.size.height)]; [scrollViewText reflectScrolledClipView:[scrollViewText contentView]];
with exactly the same problem shown here:

I fixed the problem by adding one line:
textView.string = s; [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]]; [textView scrollToEndOfDocument:self];
This runUntilDate call is not required. My theory is that it gives NSScrollView a chance to catch up and synchronize itself somehow.
It's all on the lion. I tried it with a system preference set by both "backward" scrolling and traditional scrolling to Leo with the same results.
Any ideas on:
- Why did this call that I added helped, and
- How to make it work without this call?
source share