MacOS-NSDockTile: Set the red dot on the Dock icon, nsdocktiledock
1. Set the dock icon
Through the code, we can dynamically set the Dock icon when the program is running. There are two ways to modify an icon:
Method 1: specify as an NSImage object
[NSApp setApplicationIconImage:[NSImage imageNamed:@"swift"]];
Method 2: Use a custom View to display it as the Dock icon
The custom View icon cannot be refreshed automatically. Therefore, if the Dock icon is changed, you may need to manually refresh it using the-display method when adding Badge.
NSImageView * imgView = [[NSImageView alloc] init]; imgView. frame = NSMakeRect (10, 10, 50, 50); imgView. imageFrameStyle = NSImageFramePhoto; // imgView of the image border. wantsLayer = YES; imgView. layer. backgroundColor = [NSColor cyanColor]. CGColor; imgView. image = [NSImage imageNamed: @ "swift"]; // imgView. imageScaling = NSImageScaleNone; [[NSApp dockTile] setContentView: imgView]; [[NSApp dockTile] display];
The program icons modified in the two methods described above will be restored to the application icons specified in Info. plist after the program exits.
To change the program icon permanently (that is, the modified icon can also be displayed when the program exits), you can create a plug-in for the Dock icon. This topic involves Bundle-related content and will not be detailed here.
2. Use NSDockTile to set the red dot on the Dock icon Add Badge
The configuration code is as follows:
NSDockTile *dock = [NSApp dockTile];if (dock) { [dock setBadgeLabel:@"2"]; [dock setShowsApplicationBadge:YES];}
The system automatically sets the size according to the content of BadgeLabel, as follows:
Remove Badge
To remove Badge, set the content of badgeLabel to nil.
[[NSApp dockTile] setBadgeLabel:nil];