不要缩放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