I have created several status menu (NSStatusBar
) applications before. But I have never done in Swift. If you set a custom view to a menu item, you will get a 5-point border at the bottom. I didn't think that's something you could remove. I didn't know that there's some literature on this topic till today. One topic is posted here.
Anyway, the following is what I have.
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem = NSStatusItem()
var statusImage = NSImage()
func showStatusMenu() {
statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
let img = NSImage.init(named: "StatusImage")
statusItem.image = img!.resize(w: 18.0, h: 18.0)
statusItem.menu = statusMenu
let topMenuItem = NSMenuItem(title: "", action: nil, keyEquivalent: "")
let viewRect = NSMakeRect(0, 0, 1, 30)
let menuItemView = TopBackground.init(frame: viewRect)
menuItemView.autoresizingMask = .viewWidthSizable
topMenuItem.view = menuItemView
statusMenu.addItem(topMenuItem)
}
}
class TopBackground: NSView {
override init(frame frameRect: NSRect) {
super.init(frame:frameRect);
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func draw(_ rect:NSRect) {
var fullBounds = self.bounds
fullBounds.size.height += 4
NSBezierPath(rect: fullBounds).setClip()
NSColor.blue.set()
NSRectFill(fullBounds)
super.draw(rect)
}
}
Anyway, I cannot remove the gap at the bottom. What am I doing wrong?
Muchos thankos.