1
votes

I did a little demo to isolate a problem I faced in a Project.

When I start the Application, the Cells of the NSOutlineView are too narrow for the text:

Initial View

Then I resize the window with the mouse, making it even narrower than the contents of the NSOutlineView:

Narrowed View

When I now enlarge the window again, the problem is cured. From now on the outline works as expected:

Re-Enlarged View

This is the main method of my AppDelegate:

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    NSRect frame = NSMakeRect(0., 0., 400., 300.);
    NSUInteger styleMask = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
    _mainWindow = [[NSWindow alloc] initWithContentRect:frame styleMask:styleMask backing:NSBackingStoreBuffered defer:NO];
    _mainWindow.title = @"Outline";

    NSScrollView *leftScrollView = [[NSScrollView alloc] init];
    leftScrollView.hasVerticalScroller = YES;
    leftScrollView.hasHorizontalScroller = NO;
    leftScrollView.drawsBackground = NO;
    leftScrollView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
    _mainWindow.contentView = leftScrollView;

    NSOutlineView *outlineView = [[NSOutlineView alloc] init];
    NSTableColumn *outlineColumn = [[NSTableColumn alloc] initWithIdentifier:@"Menu Item"];
    [outlineView addTableColumn:outlineColumn];
    outlineView.outlineTableColumn = outlineColumn;
    outlineView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleSourceList;
    outlineView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
    outlineView.headerView = nil;
    _outlineDataSourceAndDelegate = [[MROutlineDataSourceAndDelegate alloc] init];
    outlineView.dataSource = _outlineDataSourceAndDelegate;
    outlineView.delegate = _outlineDataSourceAndDelegate;
    leftScrollView.documentView = outlineView;

    [_mainWindow makeKeyAndOrderFront:NSApp];
}

Can anyone please explain that odd behavior?

2
I already tried using initWithFrame instead of init with a reasonable large rectangle for both the scroll view and the outline view. That didn't change anything. I also tried setting outlineView.frame before adding it to the scroll view, afterwards and after makeKeyAndOrderFront. That was not helpful, either.Michael Manner

2 Answers

0
votes

I don't know whether that is the most elegant solution, but at least it works. I added the following single line after creating the NSTableColumn:

    outlineColumn.width = SIDEBAR_WIDTH_DEFAULT;

That's it. Took me almost a day to figure out :(

0
votes

The answer above was not clean enough for me, so I continued playing around. Now I am pleased: I added a listener to the scroll view that resizes the width of the outline view to be the same as the scroll view. No more need to set the width of the outline column in advance. This is important, if the width is not known in advance.