我在NSScrollView中有一个NSTextView.我将文本放在NSTextView中并以编程方式将其滚动到底部,这可以正常工作,但滚动条保持在顶部.使用鼠标定位滚动条使其跳转到它所属的底部,从那时起它可以正常运行.
我的代码:
textView.string = s; [textView scrollToEndOfDocument:self];
不要挂在scrollToEndOfDocument方法上 – 我也尝试过:
[textView scrollRangeToVisible:NSMakeRange(s.length,0)];
和:
[[scrollViewText contentView] scrollToPoint:NSMakePoint(0,textView.frame.size.height)]; [scrollViewText reflectScrolledClipView:[scrollViewText contentView]];
完全相同的问题,如下所示:
我通过添加一行来修复问题:
textView.string = s; [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]]; [textView scrollToEndOfDocument:self];
那个runUntilDate调用不应该是必要的.我的理论是,它给NSScrollView提供了一个机会,以某种方式赶上并同步自己.
这一切都在Lion上.我尝试将系统偏好设置为Lion“向后”滚动和传统的前狮子滚动,结果相同.
任何想法:
>为什么我添加的电话有帮助,而且
>如何在没有电话的情况下使其工作?
解决方法
试试NSScrollView的reflectScrolledClipView:
编辑:如何更换
[[scrollViewText contentView] scrollToPoint:NSMakePoint(0,textView.frame.size.height)];
同
[scrollView.contentView scrollToPoint:NSMakePoint(0,scrollView.documentView.frame.size.height-scrollView.contentSize.height)];
因为它的立场我相信你滚动浏览文档视图的框架.