Adding an NSProgressIndicator to the dock icon

徘徊边缘 提交于 2019-12-03 12:51:07

In the finish I had to use the following code as the contentView was null:

    docTile = [[NSApplication sharedApplication] dockTile];
    NSImageView *iv = [[NSImageView alloc] init];
    [iv setImage:[[NSApplication sharedApplication] applicationIconImage]];
    [docTile setContentView:iv];

    progressIndicator = [[NSProgressIndicator alloc]
                                              initWithFrame:NSMakeRect(0.0f, 0.0f, docTile.size.width, 10.)];
    [progressIndicator setStyle:NSProgressIndicatorBarStyle];
    [progressIndicator setIndeterminate:NO];
    [iv addSubview:progressIndicator];

    [progressIndicator setBezeled:YES];
    [progressIndicator setMinValue:0];
    [progressIndicator setMaxValue:1];
    [progressIndicator release];

    [self setProgress:[NSNumber numberWithFloat:-1]];
}

- (void)setProgress:(NSNumber *)fraction {
    if ( [fraction doubleValue] >= 0 ) {
        [progressIndicator setDoubleValue:[fraction doubleValue]];
        [progressIndicator setHidden:NO];
    }
    else
        [progressIndicator setHidden:YES];
    [docTile display];
}

Just had a play around with the DockTile sample code: http://developer.apple.com/library/mac/#samplecode/DockTile/Introduction/Intro.html#//apple_ref/doc/uid/DTS10004391

I managed to get an NSProgress bar to display there by adding

NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, 100.0f, 20.0f)];
[self addSubview:progressIndicator];
[progressIndicator setStyle:NSProgressIndicatorBarStyle];
[progressIndicator setIndeterminate:NO];
[progressIndicator setMinValue:0];
[progressIndicator setMaxValue:100];
[progressIndicator setDoubleValue:25];
[progressIndicator release];

to SpeedometerView.m in initWithFrame, but it was still greyed out in the dock.

I also found this page: http://osx.hyperjeff.net/Apps/apps?p=4&sub=22&l=1&u=on which has "PMProgressIndicator" which might help, but I didn't dive through it.

Hope that helps a bit, post back on here if you figure it out, I'd be interested to know as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!