如何在基于视图的 NSOutlineView 中自定义 groupItem-look? [英] How do you customise the groupItem-look in a view-based NSOutlineView?

查看:121
本文介绍了如何在基于视图的 NSOutlineView 中自定义 groupItem-look?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I customise the groupItem-look in a view-based NSOutlineView? I would like to get rid of the divider border, change the background color and make the disclosure triangle dark. The background of the disclosure triangle should be the same color as the groupItem-view background.

我无法通过万能的 Google 找到任何相关信息.

I couldn't find any relevant info thru almighty Google.

推荐答案

基于视图的 NSOutlineView 使用以下 NSOutlineView*Keys 来创建用于折叠和展开项目的公开按钮".

The following NSOutlineView*Keys are used by the View Based NSOutlineView to create the "disclosure button" used to collapse and expand items.

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 提供自定义 NSButtons(或其子类):

The NSOutlineView creates these buttons by calling [self makeViewWithIdentifier:owner:] passing in the key as the identifier and the delegate as the owner. Custom NSButtons (or subclasses thereof) can be provided for NSOutlineView to use in the following two ways:

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

  1. makeViewWithIdentifier:owner: can be overridden, and if the identifier is (for instance) NSOutlineViewDisclosureButtonKey, a custom NSButton can be configured and returned. Be sure to set the button.identifier to be NSOutlineViewDisclosureButtonKey.

在设计时,可以将一个按钮添加到具有此标识符的大纲视图中,并将根据需要取消归档和使用.

At design time, a button can be added to the outlineview which has this identifier, and it will be unarchived and used as needed.

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

When a custom button is used, it is important to properly set up the target/action to do something (probably expand or collapse the rowForView: that the sender is located in). Or, one can call super to get the default button, and copy its target/action to get the normal default behavior.

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

NOTE: These keys are backwards compatible to 10.7, however, the symbol is not exported prior to 10.9 and the regular string value must be used (i.e.: @"NSOutlineViewDisclosureButtonKey").

如果您还想更改位置,请继承您的 NSTableRowView 并覆盖布局方法

If you want to change also the position, than subclass your NSTableRowView and overwrite layout method

- (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 的代码

and the code for the overwritten 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;
}

这篇关于如何在基于视图的 NSOutlineView 中自定义 groupItem-look?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆