如何在基于视图的NSOutlineView中自定义groupItem-look?

时间:2015-02-25 10:31:25

标签: cocoa swift osx-yosemite

如何在基于视图的NSOutlineView中自定义groupItem-look?我想摆脱分隔线边框,改变背景颜色,使透露三角形变暗。显示三角形的背景应与groupItem-view背景颜色相同。

我无法通过全能的谷歌找到任何相关信息。

2 个答案:

答案 0 :(得分:0)

要自定义(或隐藏)三角形按钮,只需覆盖NSOutlineView类。兴趣方法是- (id)makeViewWithIdentifier:(NSString *)identifier owner:(id)owner

// your NSOutlineView child class

- (id)makeViewWithIdentifier:(NSString *)identifier owner:(id)owner
{
   id view = [super makeViewWithIdentifier:identifier owner:owner];
   if ([identifier isEqualToString:@"NSOutlineViewDisclosureButtonKey"])
     {
      NSButton *triangleButton = (NSButton *)view;
      NSImage *image = [[NSImage alloc] init]; // you could set another images
      [triangleButton setImage:image];
      [triangleButton setAlternateImage:image];
     }
   return view;
}

现在必须隐藏三角形按钮。但是您的可扩展项目仍然存在缩进问题。

打开Interface Builder并选择大纲视图实例。打开属性检查器,将“缩进”属性设置为零。

<强>更新

在组项视图中,三角形后面有一个空白区域。只是为了删除它,如上所述,将缩进属性设置为零。

要设置自定义分隔符,只需删除NSOutlineView的自定义分隔符并自行绘制(在“单元格”类的drawRect方法中),如果要自定义公开按钮,请在NSView中实现自己的分隔符 - “细胞“亚类。

答案 1 :(得分:0)

以下NSOutlineView *键由View Based NSOutlineView用于创建用于折叠和展开项目的“公开按钮”。

APPKIT_EXTERN NSString * const NSOutlineViewDisclosureButtonKey NS_AVAILABLE_MAC(10_9); // The normal triangle disclosure button
APPKIT_EXTERN NSString * const NSOutlineViewShowHideButtonKey NS_AVAILABLE_MAC(10_9); // The show/hide button used in "Source Lists"

NSOutlineView通过调用[self makeViewWithIdentifier:owner:]来传递密钥作为标识符并将代理作为所有者来创建这些按钮。可以为NSOutlineView提供自定义NSButton(或其子类),以便以下两种方式使用:

  1. makeViewWithIdentifier:owner:可以被覆盖,如果标识符是(例如)NSOutlineViewDisclosureButtonKey,则可以配置并返回自定义NSButton。务必将button.identifier设置为NSOutlineViewDisclosureButtonKey。

  2. 在设计时,可以将一个按钮添加到具有此标识符的outlineview中,并将根据需要取消存档并使用。

    使用自定义按钮时,正确设置目标/操作以执行某些操作非常重要(可能会扩展或折叠发件人所在的rowForView:)。或者,可以调用super来获取默认按钮,并复制其目标/操作以获得正常的默认行为。

    注意:这些密钥向后兼容10.7,但是,符号不会在10.9之前导出,并且必须使用常规字符串值(即:@“NSOutlineViewDisclosureButtonKey”)。

  3. 如果你想改变位置,也要改变NSTableRowView的子类并覆盖布局方法

    - (void)layout {
    
        [super layout];
    
        for (NSView * v in self.subviews) {
            if ([v.identifier isEqual:NSOutlineViewDisclosureButtonKey]) {
                v.frame = NSMakeRect(self.bounds.size.width - 44, 0, 44, self.bounds.size.height);
                v.hidden = NO;
                break;
            }
        }
    
    }
    

    和覆盖的NSOutlineView的代码

    - (NSView *)makeViewWithIdentifier:(NSString *)identifier owner:(id)owner {
    
        NSView * v = [super makeViewWithIdentifier:identifier owner:owner];
    
        if ([identifier isEqual:NSOutlineViewDisclosureButtonKey] && ([v isKindOfClass:[NSButton class]])) {
    
            MenuDisclosureButton * b = [[MenuDisclosureButton alloc] initWithFrame:NSMakeRect(0, 0, 44, 44)];
            b.target = [(NSButton *)v target];
            b.action = [(NSButton *)v action];
            b.identifier = NSOutlineViewDisclosureButtonKey;
            v = b;
    
        }
    
        return v;
    }