简体   繁体   English

如何更改 NSSplitView 中分隔线的颜色?

[英]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. Apple 文档说,我们可以为此在 NSSplitView 的子类中覆盖 -dividerColor ,但它对我不起作用,或者我的理解不正确。 Also I've try create color layer over divider, eg:我也尝试在分隔符上创建颜色层,例如:

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.不行。

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.如果您使用的是 Interface Builder,则可以通过转到NSSplitView的 Identity Inspector ( cmd + alt + 3 ) 并为 Color 类型的dividerColor添加用户定义的运行时属性来更改该属性。

Actually, simply subclassing NSSplitView and overriding -(void)dividerColor works, but works only for thin or thick divider.实际上,只需NSSplitView并覆盖-(void)dividerColor ,但仅适用于薄或厚的分隔线。

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.然后在 Interface Builder 中为您的拆分视图指定自定义类为CustomSplitView并添加新的用户定义的运行时属性,key path = DividerColor,type = Color 并选择所需的拆分器颜色。

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).我也尝试过子类化- (void)dividerColor并且我不确定为什么它不起作用,即使我知道它被调用(并且它在文档中)。

One way to change the color of the divider is to subclass - (void)drawDividerInRect:(NSRect)aRect .更改分隔线颜色的一种方法是子类化- (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 .然而,由于某种原因,这个方法没有被调用,我在网上到处找答案,但找不到任何东西,所以我最终从drawRect调用它。 Here is the code for the subclassed NSSplitView:这是子类 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);
}

Based on Palle's answer, but with the possibility to change the color dynamically in code, I'm currently using this solution (Swift 4):基于 Palle 的回答,但可以在代码中动态更改颜色,我目前正在使用此解决方案(Swift 4):

splitView.setValue(NSColor.red, forKey: "dividerColor")

If your splitview control is part of a NSSplitViewController, you should use something like this:如果你的 splitview 控件是 NSSplitViewController 的一部分,你应该使用这样的东西:

splitViewController?.splitView.setValue(NSColor.red, forKey: "dividerColor")

One important point I haven't seen mentioned anywhere is that if you are overriding drawRect in a split view then you must call super -- otherwise drawDividerInRect: is never called.我没有在任何地方看到的一个重要点是,如果您在拆分视图中覆盖 drawRect,那么您必须调用 super —— 否则 drawDividerInRect: 永远不会被调用。 So, it should go something like this:所以,它应该是这样的:

- (void)drawRect:(NSRect)dirtyRect {
    // your other custom drawing

    // call super last to draw the divider on top
    [super drawRect:dirtyRect];

}

- (void)drawDividerInRect:(NSRect)aRect {
    [[NSColor blueColor] set];
    NSRectFill(aRect);
}

In Swift and on macOS 11 I was able to achieve this by simply subclassing the NSSPlitView and only override drawDivider()在 Swift 和 macOS 11 上,我可以通过简单地继承 NSSPlitView 并仅覆盖 drawDivider() 来实现这一点

import Foundation
import AppKit

class MainSplitView: NSSplitView {
    override func drawDivider(in rect: NSRect) {
        NSColor(named: "separatorLinesColor")?.setFill()
        rect.fill()
    }
}

I had previously tried some of the other way, listed in here and what used to work stopped working with macOS 11... but it seems that this works.我之前曾尝试过其他一些方法,列在此处,曾经可以使用的方法在 macOS 11 上停止工作......但似乎这有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM