在标准NSWindow上创建自定义标题栏 [英] Creating a custom title bar on a standard NSWindow

查看:880
本文介绍了在标准NSWindow上创建自定义标题栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力为我的菜单应用建立一个特定的外观。

I've been trying to build a specific look for my menubar app.

我一直使用 NSWindow NSBorderlessWindowMask 样式掩码和设置 [window setOpaque:NO] [window setBackgroundColor:[NSColor clearColor]]

I've been using a NSWindow with a NSBorderlessWindowMask style mask and setting [window setOpaque:NO] and [window setBackgroundColor:[NSColor clearColor]]. That gives me a blank canvas which works great for the title bar.

现在我在基于视图的 NSTableView 中遇到问题了。 c $ c>我正在使用的列表。如何将 NSTableCellView 剪辑到窗口的圆角?

Now I'm having problems with the view-based NSTableView I'm using for the listing. How can I clip the NSTableCellViews to the window's rounded corners?

我开始只是有一个自定义视图 NSTableView ,用圆角绘制背景。使用 [view addClip:path] 虽然不会剪辑子视图。

I started out just having a custom view wrapping the NSTableView, drawing the background with rounded corners. Using [view addClip:path] doesn't clip child views though.

[view setWantsLayer:YES] 使用遮罩。这工作很好,但表视图细胞会偶尔毛刺。看起来有一个 NSScrollView 是一个子层的一个已知的问题:

I've also tried using a [view setWantsLayer:YES] with a mask. That worked great, but the table view cells would sporadically glitch out. It seems that having a NSScrollView be a child of a layer is a known problem:

我当前的视图结构看起来像:

My current view structure looks something like:

NSWindow
- MyTitleBarView
- MyBackgroundView
  - NSScrollView
    - NSTableView


推荐答案

方法:

诀窍是保持窗口样式为默认值,而不是设置 NSBorderlessWindowMask 。然后你可以添加你的自定义标题栏视图到窗口的主题框架像这样:

The trick is to keep the window style as the default and not set NSBorderlessWindowMask. Then you can add your custom title bar view to the window's theme frame like so:

NSView *themeFrame = [[window contentView] superview];
NSView *firstSubview = [[themeFrame subviews] objectAtIndex:0];
[titleBarView setAutoresizingMask:(NSViewMinYMargin | NSViewWidthSizable)];
[themeFrame addSubview:titleBarView positioned:NSWindowBelow relativeTo:firstSubview];

这基本上只是将您的自定义标题栏视图放在标准标题栏的顶部。你可能需要对视图框架和窗口按钮进行一些重新调整。有关此操作的一些代码示例,请参见 INAppStoreWindow

This basically just puts your custom title bar view on top of the standard title bar. You'll probably have to do some rejiggering to the view frames and window buttons. See INAppStoreWindow for some code examples of this.

INAppStoreWindow项目说这个方法不使用任何私有API,因此能够在App Store上使用。

The INAppStoreWindow project says that this method doesn't use any private APIs, and thus is able to be used on the App Store.

如果你需要窗口透明,可以在窗口上设置以下内容:

If you require the window to be transparent, you can just set the following on the window:

[window setOpaque:NO];
[window setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]];

这篇关于在标准NSWindow上创建自定义标题栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆