31

I am porting an app from Ipad to mac. (I know that it sounds weird)

I stuck with NSScrollview. Please guide me contentsize , contentOffset equivalent in NSScrollview.

aepryus
  • 4,715
  • 5
  • 28
  • 41
shakthi
  • 1,597
  • 2
  • 17
  • 29

3 Answers3

61
UIScrollView* uiScroll;
uiScroll.contentSize;
uiScroll.contentOffset;
uiScroll.contentSize = CGSizeMake(w,h);
uiScroll.contentOffset = CGPointMake(x,y);

=

NSScrollView* nsScroll;
nsScroll.documentView.frame.size;
nsScroll.documentVisibleRect.origin;
nsScroll.documentView.frameSize = NSMakeSize(w,h);
[nsScroll.documentView scrollPoint:NSMakePoint(x,y)];

Or perhaps even better:

import AppKit

extension NSScrollView {
    var documentSize: NSSize {
        set { documentView?.setFrameSize(newValue) }
        get { documentView?.frame.size ?? NSSize.zero }
    }
    var documentOffset: NSPoint {
        set { documentView?.scroll(newValue) }
        get { documentVisibleRect.origin }
    }
}

Notes: I used 'documentSize' (and 'documentOffset') because 'contentSize' conflicts with an already existing property of NSScrollView.

aepryus
  • 4,715
  • 5
  • 28
  • 41
  • 1
    `nsScroll.documentView.scrollPoint` is not available. It shall be `[nsScroll.documentView.scrollPoint NSMakePoint(x,y)]` instead. – Harry Ng Jan 19 '16 at 14:22
  • 1
    @HarryNg Thanks, I recently modernized the syntax and created that mistake. I've fixed it now. – aepryus Jan 19 '16 at 17:09
  • There's a subtle difference between the `UIScrollView.contentOffset` setter and `NSView.scroll(_:)`: the latter makes the scrollers flash. If anyone finds out how to not make them flash, please advise. – Nickkk Dec 30 '21 at 10:35
0

In addition to the lines from @aepryus, here are a couple more useful lines for getting/setting the scroll offset on macOS:

//Get the current scroll offset:
_contentViewOffset = scrollView.contentView.bounds.origin;

//Set the scroll offset from the retrieved point:
NSPoint scrollPoint = [scrollView.contentView convertPoint:_contentViewOffset toView:scrollView.documentView];
[scrollView.documentView scrollPoint:scrollPoint];
Sheamus
  • 6,506
  • 3
  • 35
  • 61
-3

Everything you need to know about NSScrollView is laid out in the Scroll View Programming Guide for Cocoa provided in the documentation.

Although it doesn't appear that there's a direct equivalent, UIScrollView's contentSize can be likened to the size of NSScrollView's documentView, which is the scrollable content provided as an NSView to NSScrollView with setDocumentView:.

setContentOffset: can be compared to NSView's scrollPoint:, which uses an NSPoint to specify the offset of the documentView within the NSScrollView.

See the documentation for elaboration and code examples.

Dan Sandland
  • 7,095
  • 2
  • 29
  • 29