NSScrollView scrollToTop

后端 未结 4 1516
一向
一向 2021-02-14 16:55

Does anyone know the correct way to scroll the NSScrollView to the top? I was looking for an equivalent to the UIView\'s scrollToTop method.

This is what I have so far,

相关标签:
4条回答
  • 2021-02-14 17:42

    Finally figured it out. Works like a charm!

    if ([self hasVerticalScroller]) {
        self.verticalScroller.floatValue = 0;
    }
    
    NSPoint newOrigin = NSMakePoint(0, NSMaxY(((NSView*)self.documentView).frame) - self.boundsHeight);
    [self.contentView scrollToPoint:newOrigin];
    
    0 讨论(0)
  • 2021-02-14 17:49

    Updated for Swift 5:

    If your document view is flipped:

    scrollView.documentView?.scroll(.zero)
    

    otherwise:

    if let documentView = scrollView.documentView {
            documentView.scroll(NSPoint(x: 0, y: documentView.bounds.size.height))
    }
    
    0 讨论(0)
  • 2021-02-14 17:52

    The previous solutions scroll to the bottom when translating to Swift for Mac OS. The y coordinate direction is flipped on Mac OS and iOS.

    Swift 3 Solution for Mac OS:

    scrollView.documentView?.scroll(NSPoint.zero)
    
    0 讨论(0)
  • 2021-02-14 17:53

    This also works fine. Set scroll point of documentView of NSScrollView

    NSPoint pt = NSMakePoint(0.0, [[self.scrollView documentView]
                                   bounds].size.height);
    [self.scrollView.documentView scrollPoint:pt];
    
    0 讨论(0)
提交回复
热议问题