Reputation: 73
I am trying to emulate the behaviour of a NSMenu using a NSWindow. However, when I tried anchoring the window just below the status item (at the top of the screen) I became aware that by default all windows have a 1 pixel margin between the top of the window and the status bar. Here is what I mean: NSWindow at top of screen with unintended margin
I have found an outdated implementation on github that manages to create a custom window below the status bar without this margin, but I am having a hard time figuring out how the implementation achieves this. https://github.com/SquaredTiki/JGMenuWindow
How can I get rid of the margin so that the window fits snuggly against the status bar?
Upvotes: 2
Views: 514
Reputation: 449
It appears that you try to attach your "menu" to an NSStatusItem
. Is there a reason you don't actually attach a menu and set the respective NSMenuItem
's view to be whatever you want it to look like? Apple has an example here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MenuList/Articles/ViewsInMenuItems.html
Upvotes: 0
Reputation: 73
Found the solution!
override func constrainFrameRect(frameRect: NSRect, toScreen screen: NSScreen?) -> NSRect {
return frameRect
}
Override in your NSWindow subclass. Now you can move your NSWindow in any way you wish and it will be able to go above the menubar. This is because this function is normally invoked just before the window is moved to check if the proposed new window position has it's top edge lying on the screen. If this is not the case, the function will return an adjusted position in which the top edge does lie on the screen. By overriding this function and returning frameRect you are not doing any checks or readjustments to the new proposed window position, you are telling the window it can move anywhere.
Upvotes: 4