简体   繁体   English

NSPopover 显示相对于 NSStatusItem

[英]NSPopover show relative to NSStatusItem

This is my code:这是我的代码:

if #available(OSX 10.10, *) {
    if let b = statusItem.button {
        popover.showRelativeToRect(b.bounds, ofView: b, preferredEdge: .MinY)
    }
} else {
}

The else block is for OS X Mavericks because NSStatusItem.button is not available. else块用于 OS X Mavericks,因为NSStatusItem.button不可用。 Is there a simple way of showing the popover relative to the status item?有没有一种简单的方法来显示相对于状态项的弹出框? If not, is it possible to show the popover in the center of the screen instead without the arrow?如果没有,是否可以在没有箭头的情况下在屏幕中央显示弹出窗口?

before you had access to the statusitem button you had to provide your own view.在您访问 statusitem 按钮之前,您必须提供您自己的视图。 Then all works the same然后一切都一样


to retain the original behaviour, draw a custom view that looks like a status item ;)要保留原始行为,请绘制一个看起来像状态项的自定义视图;)

eg例如

@interface DDQuickMenuStatusItemView : NSView
@property(weak) NSStatusItem *item;
//...
@end


@implementation DDQuickMenuStatusItemView

//...

- (void)drawRect:(NSRect)dirtyRect {
    NSImage *image = nil;
    if(self.item) {
        [self.item drawStatusBarBackgroundInRect:self.bounds withHighlight:NO];
        image = self.item.image;
    }

    if(image) {
        NSRect r = self.bounds;
        r.size = [image size];
        r = [self.class centerRect:r inRect:self.bounds];
        r = [self centerScanRect:r];
        [image drawInRect:r fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
    }
}

#pragma mark -

+ (CGRect)centerRect:(CGRect)rect inRect:(CGRect)inRect
{
    CGRect result = rect;
    result.origin.x = inRect.origin.x + (inRect.size.width - result.size.width)*0.5f;
    result.origin.y = inRect.origin.y + (inRect.size.height - result.size.height)*0.5f;
    return result;
}

@end

Note that the view is a sample and not production ready ;)请注意,该视图是一个示例,尚未准备好用于生产;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM