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;
.fullSizeContentView on NSWindow
if let windowFrameHeight = self.view.window?.contentView?.frame.height,
let contentLayoutRectHeight = self.view.window?.contentLayoutRect.height {
let fullSizeContentViewNoContentAreaHeight = windowFrameHeight - contentLayoutRectHeight
}
fullSizeContentViewNoContentAreaHeight is the height you want.
This is important to compute this value dynamically, because if you have an app using NSDocument and if the user creates a new document, the system might open this new document in a new tab.
You might do this in updateViewConstraints() to detect this kind of changes.
Source: https://developer.apple.com/videos/play/wwdc2016/239/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With