Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color of divider in NSSplitView?

Can we change the color of the divider? Apple documentations says, that we can override -dividerColor in subclass of NSSplitView for this, but it doesn't works for me, or my understanding isn't correct. Also I've try create color layer over divider, e.g.:

colorLayer = [CALayer layer];
NSRect dividerFrame = NSMakeRect([[self.subviews objectAtIndex:0] frame].size.width, [[self.subviews objectAtIndex:0] frame].origin.y, [self dividerThickness], self.frame.size.height);

[colorLayer setBackgroundColor:[color coreGraphicsColorWithAlfa:1]];
[colorLayer setFrame:NSRectToCGRect(dividerFrame)];

[self.layer addSublayer:colorLayer];

Not works.

like image 371
Akki Avatar asked Jun 05 '12 08:06

Akki


3 Answers

This answer may be late but:
If you are using Interface Builder, it is possible to change the property by going to the Identity Inspector of the NSSplitView (cmd+alt+3) and adding a User Defined Runtime Attribute for dividerColor of the type Color.

like image 117
Palle Avatar answered Nov 14 '22 09:11

Palle


Actually, simply subclassing NSSplitView and overriding -(void)dividerColor works, but works only for thin or thick divider.

I've created simple configurable split view like this:

@interface CustomSplitView : NSSplitView
@property NSColor* DividerColor
@end

@implementation CustomSplitView
- (NSColor*)dividerColor {
  return (self.DividerColor == nil) ? [super dividerColor] : self.DividerColor;
}
@end

Then in Interface Builder specify custom class for your split view to be CustomSplitView and add new user defined runtime attribute with key path = DividerColor, type = Color and select desired splitter color.

like image 21
Sergio Avatar answered Nov 14 '22 08:11

Sergio


I've tried subclassing - (void)dividerColor too and I'm not sure why it doesn't work even though I know it's being called (and it's in the documentation).

One way to change the color of the divider is to subclass - (void)drawDividerInRect:(NSRect)aRect. However, for some reason, this method isn't called and I've checked all over the web for answers, but couldn't find anything, so I ended up calling it from drawRect. Here is the code for the subclassed NSSplitView:

-(void) drawRect {
    id topView = [[self subviews] objectAtIndex:0];
    NSRect topViewFrameRect = [topView frame];
    [self drawDividerInRect:NSMakeRect(topViewFrameRect.origin.x, topViewFrameRect.size.height, topViewFrameRect.size.width, [self dividerThickness] )];
}

-(void) drawDividerInRect:(NSRect)aRect {
    [[NSColor redColor] set];
    NSRectFill(aRect);
}
like image 7
uff da Avatar answered Nov 14 '22 08:11

uff da