Scrolling NSTextView leaves scrollbar in wrong position

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:

Scroll bar in wrong position.

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?
+4
source share
1 answer

Try NSScrollView reflectScrolledClipView:

Edit: how about replacing [[scrollViewText contentView] scrollToPoint:NSMakePoint(0, textView.frame.size.height)];

with

[scrollView.contentView scrollToPoint:NSMakePoint(0, scrollView.documentView.frame.size.height-scrollView.contentSize.height)];

Because, since that means you are scrolling through the document viewing frame.

+3
source

Source: https://habr.com/ru/post/1380632/


All Articles