1 简述
工具栏,用于管理窗口标题栏下方和应用程序的自定义内容上方的空间,以快速访问应用程序功能。
2 源码注解
@interface NSToolbar : NSObject
|
2.1 初始化创建
- (instancetype)initWithIdentifier:(NSToolbarIdentifier)identifier NS_DESIGNATED_INITIALIZER; - (instancetype)init API_AVAILABLE(macos(10.13));
|
2.2 代理
@property (nullable, weak) id<NSToolbarDelegate> delegate;
@protocol NSToolbarDelegate <NSObject>
@optional - (nullable NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSToolbarItemIdentifier)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
- (NSArray<NSToolbarItemIdentifier> *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar;
- (NSArray<NSToolbarItemIdentifier> *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar;
@optional
- (NSArray<NSToolbarItemIdentifier> *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
- (void)toolbarWillAddItem:(NSNotification *)notification;
- (void)toolbarDidRemoveItem:(NSNotification *)notification;
|
2.3 属性
@property (readonly, copy) NSToolbarIdentifier identifier;
@property NSToolbarDisplayMode displayMode; typedef NS_ENUM(NSUInteger, NSToolbarDisplayMode) { NSToolbarDisplayModeDefault, NSToolbarDisplayModeIconAndLabel, NSToolbarDisplayModeIconOnly, NSToolbarDisplayModeLabelOnly } API_AVAILABLE(ios(13.0));
@property BOOL showsBaselineSeparator;
@property BOOL allowsUserCustomization;
@property BOOL allowsExtensionItems API_AVAILABLE(macos(10.10)) API_UNAVAILABLE(ios);
@property (readonly, copy) NSArray<__kindof NSToolbarItem *> *items;
@property (nullable, readonly, copy) NSArray<__kindof NSToolbarItem *> *visibleItems;
@property NSToolbarSizeMode sizeMode API_UNAVAILABLE(ios); typedef NS_ENUM(NSUInteger, NSToolbarSizeMode) { NSToolbarSizeModeDefault, NSToolbarSizeModeRegular, NSToolbarSizeModeSmall };
|
2.4 管理item
- (void)insertItemWithItemIdentifier:(NSToolbarItemIdentifier)itemIdentifier atIndex:(NSInteger)index;
- (void)removeItemAtIndex:(NSInteger)index;
|
3 重点解析
3.1 sizeMode
设置工具栏图标大小
- NSToolbarSizeModeDefault // 默认大小
- NSToolbarSizeModeRegular // 常规尺寸 32*32像素
- NSToolbarSizeModeSmall // 小尺寸24*24像素
[toolBar setSizeMode:NSToolbarSizeModeSmall];
NSToolbar *toolBar = [[NSToolbar alloc] initWithIdentifier:@"toolBar"]; [self.view.window setToolbar:toolBar]; switch (toolBar.sizeMode) { case NSToolbarSizeModeDefault: break; case NSToolbarSizeModeRegular: break; case NSToolbarSizeModeSmall: break; default: break; }
|
3.2 displayMode
设置工具栏显示模式
- NSToolbarDisplayModeDefault // 默认
- NSToolbarDisplayModeIconAndLabel // 显示图标和标签
- NSToolbarDisplayModeIconOnly // 只显示图标
- NSToolbarDisplayModeLabelOnly // 只显示标签
[toolBar setDisplayMode:NSToolbarDisplayModeIconOnly];
|
4 示例
4.1 代码
创建继承NSWindowController
的窗口控制器FSWindowCtl,并在Main.storyboard中修改为FSWindowCtl,如下图
在FSWindowCtl
添加item
标识符
static NSToolbarItemIdentifier leftIdentifier = @"left"; static NSToolbarItemIdentifier rightIdentifier = @"right";
|
windowDidLoad
中添加并设置NSToolBar
- (void)windowDidLoad { [super windowDidLoad]; NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"toolbar"]; [toolbar setSizeMode:NSToolbarSizeModeDefault]; toolbar.allowsUserCustomization = YES; toolbar.autosavesConfiguration = YES; toolbar.displayMode = NSToolbarDisplayModeIconAndLabel; toolbar.delegate = self; [self.window setToolbar:toolbar]; }
|
实现toolbar的代理方法
#pragma mark - NSToolbarDelegate - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSToolbarItemIdentifier)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag { NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] init]; if ([itemIdentifier isEqualToString:leftIdentifier]) { toolbarItem = [self setToolbarItem:@"left" label:@"left" paletteLable:@"left" toolTip:@"left tip" image:@"left"]; } else if ([itemIdentifier isEqualToString:rightIdentifier]) { toolbarItem = [self setToolbarItem:@"right" label:@"right" paletteLable:@"right" toolTip:@"right tip" image:@"right"]; } else { return nil; } return toolbarItem; }
- (NSToolbarItem *)setToolbarItem:(NSString *)identifier label:(NSString *)label paletteLable:(NSString *)paletteLable toolTip:(NSString *)toolTip image:(NSString *)image { NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:identifier]; toolbarItem.label = label; toolbarItem.paletteLabel = paletteLable; toolbarItem.toolTip = toolTip; toolbarItem.target = self; [toolbarItem setAction:@selector(itemClick:)]; toolbarItem.image = [NSImage imageNamed:image]; return toolbarItem; }
- (NSArray<NSToolbarItemIdentifier> *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar { return @[NSToolbarSpaceItemIdentifier, leftIdentifier, rightIdentifier, NSToolbarSpaceItemIdentifier, NSToolbarShowColorsItemIdentifier]; }
- (NSArray<NSToolbarItemIdentifier> *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar { return @[leftIdentifier, rightIdentifier, NSToolbarShowColorsItemIdentifier, NSToolbarSpaceItemIdentifier]; }
#pragma mark - Action - (void)itemClick:(NSToolbarItem *)item { }
|
4.2 storyboard
直接在storyboard拖拽NSToolbarItem,并添加即可,不再详述。
toolbarItem.view = 自定义的View
|
FSToolbarDemo