Dirk-Willem van Gulik
Dirk-Willem van Gulik

Reputation: 7706

NSTabView with background color

As discussed elsewhere, NSTabView does not have a setBackgroundColor method and subclassing NSTabView and using an drawRect to control it does no longer work - as it does not paint the top 10%, the bit just below the segmented control button.

Now I am a bit surprised by the amounts of work arounds I had to do solving this; see

and am wondering if i went down the wrong path. And how to do this better & simpler:

Feedback appreciated - as this feels so far off/suboptimal for such a simple things -- Thanks a lot. Brownie points for hacking/forking the github code :) :) :) As a line of running code says more than a thousand words.

Dw.

Upvotes: 3

Views: 4028

Answers (3)

spinacher
spinacher

Reputation: 549

If your situation can tolerate some fragility, a very simple and quick approach is to subclass NSTabView and manually adjust the frame of the item subviews. This gives each item a seamless yellow background:

- (void)drawRect:(NSRect)dirtyRect {
    static const NSRect offsetRect = (NSRect) { -2, -16, 4, 18 };

    NSRect rect = self.contentRect;

    rect.origin.x += offsetRect.origin.x;
    rect.origin.y += offsetRect.origin.y;
    rect.size.width += offsetRect.size.width;
    rect.size.height += offsetRect.size.height;

    [[NSColor yellowColor] set];
    NSRectFill(rect);

    [super drawRect:dirtyRect];
}

A future change in the metrics of NSTabView would obviously be a problem so proceed at your own risk!

Upvotes: 0

dgatwood
dgatwood

Reputation: 10417

Have you tried setting the background color of its underlying CALayer? (Make it a layer-backed view, if it isn't already, by setting wantsLayer = YES.)

Upvotes: 0

Doc Maynord
Doc Maynord

Reputation: 26

PSMTabBarControl is probably the best workaround for you. I have created several custom tab views, but cocoa does not play well with this control. PSMTabBarControl has been updated to support Xcode 4. https://github.com/ciaran/psmtabbarcontrol

Upvotes: 1

Related Questions