I have an empty NSScrollView created with Interface Builder.
And in my code I add, upon a user click, a NSSegmentedControl. For all the next clicks, I add a segment to it.
My problem is that, once I reach the visual limit of NSScrollView, it doesn't start to scroll and all the post-limit segments are never shown.
This is the NSScrollView from Interface Builder :
And here is the method :
- (IBAction)addSegment:(id)sender
{
if (segCtrlColumns == nil) {
segCtrlColumns = [[NSSegmentedControl alloc] initWithFrame:[self.viewColumns frame]]; //self.viewColumns is the NSScrollView from IBOutlet
[segCtrlColumns setSegmentCount:1];
[segCtrlColumns setLabel:@"Test" forSegment:0];
[segCtrlColumns setTarget:self];
} else {
double increaseSegments = [segCtrlColumns segmentCount]+1;
[segCtrlColumns setSegmentCount:increaseSegments];
[segCtrlColumns setLabel:@"Test" forSegment:increaseSegments-1];
}
[self.viewColumns setDocumentView:segCtrlColumns];
}
And here is the problem :
(Well it is more likely "nothing to scroll" than "not scrollable")
Okay, I did a few tests and the problem is not the nsscrollview but the nssegmentedcontrol. It doesn't update its frame size at all. So it is stuck at the width I set in the initWithFrame
- which was the size of the scrollView. (And this is why NSScrollView never started to scroll)
I never found an easy way on how to get the real size (updated size) of NSSegmentedControl as more segments are added, so I did this trick :
here is the code :
-(IBAction)addColumn:(id)sender
{
if (segCtrlColumns == nil) {
//Init with a single segment with 100 points (pixels) width
segCtrlColumns = [[NSSegmentedControl alloc] initWithFrame:NSMakeRect(0, 0, 100.0, 30.0)];
[segCtrlColumns setSegmentCount:1];
[segCtrlColumns setWidth:96.0 forSegment:0]; //The segment has some borders to concider in its size
[segCtrlColumns setLabel:@"Testing" forSegment:0];
[segCtrlColumns setTarget:self];
//Tell the scrollview, the documentView is my segmented control
[self.viewColumns setDocumentView:segCtrlColumns];
} else {
//Add a segment and update the frame size
double increaseSegments = [segCtrlColumns segmentCount]+1;
[segCtrlColumns setFrame:NSMakeRect(0, 0, segCtrlColumns.frame.size.width+100.0, 30.0)];
[segCtrlColumns setSegmentCount:increaseSegments];
[segCtrlColumns setWidth:96.0 forSegment:increaseSegments-1];
[segCtrlColumns setLabel:@"Testing" forSegment:increaseSegments-1];
}
}
The only problem here is that all the segments are at the same size and not dynamic...