是什么原因导致白色边框应用于系统菜单中显示的NSProgressIndicator?

时间:2010-04-26 16:07:27

标签: objective-c cocoa nsprogressindicator

当我创建NSProgressIndicator并use NSStatusItem's -setView: method在我正在执行操作时在菜单栏区域中显示它时会发生这种情况:

Example of messed up 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];

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-2)

我见过你解决了我将要解决的问题这是你实施的替代方案。您可以使用ProgressHud