0

In my cocoa program, I have drawn a NSScrollView in the nib. I want to programmatically add NSViews into this NSScrollViews so that users can scroll to see all the views even if some views are out of boundary.

Here is what I have done so far:

int startX = 23;
int startY = 157;
int verticalGap = 10;
for (int i = 0; i < 10; i++){     
    singleView *temp = [[singleDontKnowView alloc] init];    
    [temp setFrameOrigin:NSMakePoint(startX, startY + (temp.frame.size.height + verticalGap) * i)];
    [_scorllView setDocumentView:temp];
}

The question is when I run this, it can only load one singleView at position (startX, startY). How to fix this so that all 10 singleViews will be displayed?

UPDATE:

So now my code looks like:

int startX = 23;
int startY = 157;
int verticalGap = 10;
NSView *docView = [[NSView alloc] initWithFrame:NSMakeRect(0,0,369,396)];
for (int i = 0; i < 10; i++){     
     singleView *temp = [[singleDontKnowView alloc] init];    
     [temp setFrameOrigin:NSMakePoint(startX, startY + (temp.frame.size.height + verticalGap) * i)];
     [docView addSubView:temp];
}
[_scorllView setDocumentView: docView];

By running the code, I can only get an empty ScrollView.

GiddensA
  • 21
  • 7
  • You are setting scrollView.documentView to a singleView. In your code, the last singleView will be set as the documentView. You want to add all of the singleView's into a container view and then set that container view as the documentView. – rocky Jul 06 '16 at 00:36
  • I tried to add all the singleViews into an NSView, and set that NSView as the DocumentView. But nothing is on display. – GiddensA Jul 06 '16 at 10:10
  • Can you please show the code for that so we can help? – rocky Jul 06 '16 at 16:57
  • So you are setting the frameOrigin of your singleView's but you never set the frame sizes. They could be zero for all I know. Before attempting to show the container view in a scroll view, make sure you can show the container view with all the views just by itself and then put it in a scroll view. – rocky Jul 13 '16 at 23:44
  • This SO answer explains how to use `NSView` inside `NSScrollView` in Auto-layout environment (Swift): https://stackoverflow.com/a/46431542/1418981 – Vlad Feb 24 '19 at 15:53

0 Answers0