5

当我创建一个 NSProgressIndicator 并在执行操作时使用NSStatusItem'-setView:方法在菜单栏区域中显示它时会发生这种情况:

搞砸 NSProgressIndicator http://cl.ly/l9R/content 的示例

是什么导致显示此边框,我该如何删除它?预期的结果是控件是透明的。

这是我正在使用的代码:

NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] init];

[progressIndicator setBezeled: NO];
[progressIndicator setStyle: NSProgressIndicatorSpinningStyle];
[progressIndicator setControlSize: NSSmallControlSize];
[progressIndicator sizeToFit];
[progressIndicator startAnimation: self];
[statusItem setView: progressIndicator]; // statusItem is an NSStatusItem instance
...
[statusItem setView: nil];
[progressIndicator stopAnimation: self];
[progressIndicator release];
4

2 回答 2

6

不要缩放NSProgressIndicator虽然它也是一个NSView. 创建一个调整大小的新视图,将状态指示器定位在该视图中,并将封闭视图传递给-[NSStatusItem setView:]. 这是我的实现。

NSStatusItem+AnimatedProgressIndicator.m,

- (void) startAnimation {

    NSView *progressIndicatorHolder = [[NSView alloc] init];

    NSProgressIndicator *progressIndicator = [[NSProgressIndicator alloc] init];

    [progressIndicator setBezeled: NO];
    [progressIndicator setStyle: NSProgressIndicatorSpinningStyle];
    [progressIndicator setControlSize: NSSmallControlSize];
    [progressIndicator sizeToFit];
    [progressIndicator setUsesThreadedAnimation:YES];

    [progressIndicatorHolder addSubview:progressIndicator];
    [progressIndicator startAnimation:self];

    [self setView:progressIndicatorHolder];

    [progressIndicator center];

    [progressIndicator setNextResponder:progressIndicatorHolder];
    [progressIndicatorHolder setNextResponder:self];

}

- (void) stopAnimation {

    [self setView:nil];

}

- (void) mouseDown:(NSEvent *) theEvent {

    [self popUpStatusItemMenu:[self menu]];

}

- (void) rightMouseUp:(NSEvent *) theEvent {}
- (void) mouseUp:(NSEvent *) theEvent {}
…

我添加了一个自定义方法 ,-[NSView center]它执行以下操作:

@implementation NSView (Centering)

- (void) center {

    if (![self superview]) return;

    [self setFrame:NSMakeRect(

        0.5 * ([self superview].frame.size.width - self.frame.size.width),
        0.5 * ([self superview].frame.size.height - self.frame.size.height), 

        self.frame.size.width, 
        self.frame.size.height

    )];

}

@end
于 2010-05-13T18:45:42.690 回答
-2

我已经看到您解决了我将留下的问题,这是您实施的替代方案。您可以使用ProgressHud

于 2014-01-23T21:12:23.900 回答