我创建了一个 OS/X 应用程序,当它运行时,我将窗口放置在屏幕中央。为此,在计算 y 值时必须包含标题栏高度。
有没有办法确定默认标题栏?我希望(根据我使用其他窗口系统的经验)我必须以某种方式查询窗口管理器......
最佳答案
我想出了这个解决方案。注意当窗口的 styleMask
包括 fullSizeContentView
,这将返回 0.0
,因为在这种情况下,标题栏实际上没有高度。
作为 Swift 扩展:
extension NSWindow {
var titlebarHeight: CGFloat {
frame.height - contentRect(forFrameRect: frame).height
}
}
用法:let titlebarHeight = someWindow.titlebarHeight
作为 Objective-C 类别扩展:@implementation NSWindow (TitleBarHeight)
- (CGFloat) titlebarHeight
{
return self.frame.size.height - [self contentRectForFrameRect: self.frame].size.height;
}
@end
用法:CGFloat titlebarHeight = someWindow.titlebarHeight;
关于macos - 如何获得 NSWindow 的默认标题栏高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28955483/