I created an OS/X app that when it runs, I position the window in the center of the screen. In order to do this, it's essential that I include the title bar height in my calculation of the y value.
Is there a way to determine the default title bar? I'd expect (based on my experience with other windowing systems) that I have to query the window manager somehow...
I've come up with this solution. Note that when the window's styleMask
includes fullSizeContentView
, this returns 0.0
, because in that case the titlebar effectively has no height.
extension NSWindow {
var titlebarHeight: CGFloat {
frame.height - contentRect(forFrameRect: frame).height
}
}
Usage:
let titlebarHeight = someWindow.titlebarHeight
@implementation NSWindow (TitleBarHeight)
- (CGFloat) titlebarHeight
{
return self.frame.size.height - [self contentRectForFrameRect: self.frame].size.height;
}
@end
Usage:
CGFloat titlebarHeight = someWindow.titlebarHeight;