返回

MacOS 开发:用纯代码实现 NSToolbar(以迅雷顶部工具栏效果为例)

IOS

** NSToolbar 在 MacOS 开发中是一个很常见的控件,但在中文社区中却鲜有资料。大多数关于 NSToolbar 的教程都以 XIB 形式实现,而本文将深入探究如何使用纯代码实现 NSToolbar,并以迅雷顶部工具栏效果为例进行详细演示。**

在开始之前,让我们先了解一下 NSToolbar。NSToolbar 是一种可添加到应用程序窗口顶部的控件,它提供了一系列自定义按钮和控件,用户可以通过这些按钮和控件执行各种任务或访问应用程序功能。

实现纯代码 NSToolbar 的步骤如下:

  1. 创建 NSToolbar 实例
NSToolbar *toolbar = [[NSToolbar alloc] init];
  1. 设置 NSToolbar 代理
toolbar.delegate = self;
  1. 实现 NSToolbarDelegate 协议方法

NSToolbarDelegate 协议中包含了几个用于自定义工具栏的方法,包括:

  • **- (NSArray<NSToolbarItem *> )toolbarAllowedItemIdentifiers:(NSToolbar )toolbar :返回允许添加到工具栏的项目标识符数组。
  • **- (NSToolbarItem *)toolbar:(NSToolbar )toolbar itemForItemIdentifier:(NSString )itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag :为给定的项目标识符创建并返回工具栏项目。
  • **- (NSArray<NSToolbarItem *> )toolbarDefaultItemIdentifiers:(NSToolbar )toolbar :返回工具栏的默认项目标识符数组。
  1. 添加工具栏项目
NSToolbarItem *item1 = [[NSToolbarItem alloc] initWithItemIdentifier:@"item1" image:[NSImage imageNamed:@"item1"] target:self action:@selector(item1Action:)];
[toolbar insertItemWithItemIdentifier:@"item1" atIndex:0];
  1. 将 NSToolbar 添加到窗口
[window setToolbar:toolbar];

让我们以迅雷顶部工具栏效果为例,逐步实现一个 NSToolbar。迅雷顶部工具栏包含一个文件菜单、一个编辑菜单、一个视图菜单和一个帮助菜单。

首先,我们需要创建 NSToolbar 实例并设置其代理:

NSToolbar *toolbar = [[NSToolbar alloc] init];
toolbar.delegate = self;

接下来,我们实现 NSToolbarDelegate 协议中的方法:

- (NSArray<NSToolbarItem *> *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
    return @[@"fileMenu", @"editMenu", @"viewMenu", @"helpMenu"];
}

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
    if ([itemIdentifier isEqualToString:@"fileMenu"]) {
        NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
        item.image = [NSImage imageNamed:@"fileMenu"];
        item.label = @"文件";
        item.target = self;
        item.action = @selector(fileMenuAction:);
        return item;
    } else if ([itemIdentifier isEqualToString:@"editMenu"]) {
        NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
        item.image = [NSImage imageNamed:@"editMenu"];
        item.label = @"编辑";
        item.target = self;
        item.action = @selector(editMenuAction:);
        return item;
    } else if ([itemIdentifier isEqualToString:@"viewMenu"]) {
        NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
        item.image = [NSImage imageNamed:@"viewMenu"];
        item.label = @"视图";
        item.target = self;
        item.action = @selector(viewMenuAction:);
        return item;
    } else if ([itemIdentifier isEqualToString:@"helpMenu"]) {
        NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
        item.image = [NSImage imageNamed:@"helpMenu"];
        item.label = @"帮助";
        item.target = self;
        item.action = @selector(helpMenuAction:);
        return item;
    }
    return nil;
}

- (NSArray<NSToolbarItem *> *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
    return @[@"fileMenu", @"editMenu", @"viewMenu", @"helpMenu"];
}

最后,我们添加 NSToolbar 到窗口:

[window setToolbar:toolbar];

通过以上步骤,我们成功地使用纯代码实现了 NSToolbar,并且实现了迅雷顶部工具栏的效果。

在实际开发中,还可以根据需要对 NSToolbar 进行进一步的定制,例如添加分隔符、自定义工具栏大小、设置工具栏样式等。通过掌握 NSToolbar 的使用技巧,开发者可以为应用程序创建更加美观、实用的用户界面。