2012-08-15 42 views
3

有誰知道將NSScrollView滾動到頂部的正確方法嗎?我正在尋找一個相當於UIView的scrollToTop方法。NSScrollView scrollToTop

這是我到目前爲止,但它並不完全正確在所有情況下

[self.contentView scrollToPoint:NSMakePoint(0, MAX(contentView.frameHeight, ((NSView*)self.documentView).frameHeight))]; 

回答

7

終於想通了。奇蹟般有效!

if ([self hasVerticalScroller]) { 
    self.verticalScroller.floatValue = 0; 
} 

NSPoint newOrigin = NSMakePoint(0, NSMaxY(((NSView*)self.documentView).frame) - self.boundsHeight); 
[self.contentView scrollToPoint:newOrigin]; 
4

這也很好。設置NSScrollView的documentView的滾動點

NSPoint pt = NSMakePoint(0.0, [[self.scrollView documentView] 
           bounds].size.height); 
[self.scrollView.documentView scrollPoint:pt]; 
0

以前的解決方案在翻譯爲Swift for Mac OS時滾動到底部。在Mac OS和iOS上翻轉y座標方向。適用於Mac OS

斯威夫特3解決方法:

scrollView.documentView?.scroll(NSPoint.zero) 
+0

您的解決方案滾動至底部。也許你有你的視圖座標翻轉? – JanApotheker 2017-03-26 11:32:29

+0

啊,澄清。我的解決方案適用於Mac OS。座標在iOS上翻轉,因此您可能想要在iOS上使用文檔邊界高度。 – Mel 2017-03-27 17:13:29

2

斯威夫特3解決方法:

if let documentView = scrollView.documentView { 
     documentView.scroll(NSPoint(x: 0, y: documentView.bounds.size.height)) 
}