Sup*_*ppy 4 macos window-managers nswindow
我创建了一个OS/X应用程序,当它运行时,我将窗口定位在屏幕的中心.为了做到这一点,我必须在计算y值时包含标题栏高度.
有没有办法确定默认标题栏?我希望(基于我对其他窗口系统的经验)我必须以某种方式查询窗口管理器...
.fullSizeContentViewon时计算工具栏/标题栏区域高度的答案NSWindowif let windowFrameHeight = self.view.window?.contentView?.frame.height,
let contentLayoutRectHeight = self.view.window?.contentLayoutRect.height {
let fullSizeContentViewNoContentAreaHeight = windowFrameHeight - contentLayoutRectHeight
}
Run Code Online (Sandbox Code Playgroud)
fullSizeContentViewNoContentAreaHeight是你想要的高度。这对于动态计算此值很重要,因为如果您有一个应用程序正在使用NSDocument并且用户创建了一个新文档,系统可能会在新选项卡中打开这个新文档。您可以这样做updateViewConstraints()以检测此类更改。
来源:https : //developer.apple.com/videos/play/wwdc2016/239/
我想出了这个解决方案.请注意,当窗口styleMask包含时fullSizeContentView,会返回0.0,因为在这种情况下,标题栏实际上没有高度.
extension NSWindow {
var titlebarHeight: CGFloat {
let contentHeight = contentRect(forFrameRect: frame).height
return frame.height - contentHeight
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
let titlebarHeight = someWindow.titlebarHeight
Run Code Online (Sandbox Code Playgroud)
@implementation NSWindow (TitleBarHeight)
- (CGFloat) titlebarHeight
{
CGFloat contentHeight = [self contentRectForFrameRect: self.frame].size.height;
return self.frame.size.height - contentHeight;
}
@end
Run Code Online (Sandbox Code Playgroud)
用法:
CGFloat titlebarHeight = someWindow.titlebarHeight;
Run Code Online (Sandbox Code Playgroud)
尚不清楚是要居中对齐矩形,然后构建框架rect以使内容rect居中,还是要居中对齐矩形并对感兴趣的内容rect感兴趣。
无论哪种情况,NSWindow都有可以提供帮助的方法。在拥有实例之前,可以使用类方法+frameRectForContentRect:styleMask:和+contentRectForFrameRect:styleMask:。这些考虑了由样式蒙版表示的窗口样式,但没有考虑最终窗口可能包含的任何工具栏。
如果你使用现有的实例时,您可以使用的方法-frameRectForContentRect:和-contentRectForFrameRect:。这些使用窗口的当前样式,并考虑其工具栏。(工具栏位于框架区域内,但不在内容区域内。)
您似乎确定要使用窗口的实际屏幕中心。但是,您应该考虑使用的-center方法NSWindow。它将窗口水平居中放置,但实际上比屏幕的真正垂直居中位置高。这样做是故意的,因为对于用户而言,这被认为更加突出和直接。
| 归档时间: |
|
| 查看次数: |
5449 次 |
| 最近记录: |