带有“标题栏”的 NSPanel在 Interface Builder 中取消选中不会出现

发布于 2024-12-07 20:06:38 字数 989 浏览 1 评论 0原文

所以我以为我已经完成了我的基础,但显然我错过了一两个关键步骤。

我有一个 NSPanel,当按下 NSStatusItem 时会显示 (makeKeyAndOrderFront:)。一切都很好,但是当 NSPanel 显示标题栏时,面板也是可拖动的。 (这是不希望的。)

第一个屏幕截图显示了在 Interface Builder 的“外观”类别中启用“标题栏”的面板。 (抱歉,内容有些模糊,目前一切仍处于锁定状态。)

第一张截图

唯一

在此处输入图像描述

我尝试过的事情:

  • 我已经对 NSPanel 进行了子类化经过一番研究后,为 canBecomeKeyWindowcanBecomeMainWindow 返回了 YES,但是(在子类化之前)这些方法都返回了NO 无论我是否使用标题栏。所以我不认为这是问题所在。

  • 我已确保 NSPanel 的框架已正确设置。它具有良好的高度,并且原点也设置正确。

编辑:忘记提及:

该应用程序是一个仅包含菜单栏的应用程序。在下面的屏幕截图中,请注意,在 Info.plist 中添加了一个附加条目来强制执行此操作。

信息列表

So I thought I had covered my bases, but apparently I'm missing a key step or two.

I have an NSPanel that is displayed (makeKeyAndOrderFront:) when an NSStatusItem is pressed. Things work great, but as the NSPanel displays a title bar, the panel is also draggable. (This is undesired.)

The first screenshot shows the panel with "Title Bar" enabled in Interface Builder, in the Appearance category. (Sorry for the blur, things are still under lock and key for now.)

First screenshot

The only change that is made in Interface Builder is unchecking the "Title Bar" checkbox. I then save and re-run, and that's what you see in the second screenshot. While a slight shadow appears, the panel does not.

enter image description here

Things I've tried:

  • I've subclassed the NSPanel and returned YES for canBecomeKeyWindow and canBecomeMainWindow after a bit of research, but (prior to subclassing) these methods both returned NO regardless of whether I was using a Title Bar or not. So I don't think this is the issue.

  • I've ensured that the frame for the NSPanel is properly set. It has a good height, and the origin is set properly as well.

Edit: Forgot to Mention:

The application is a menu-bar-only application. In the screenshot below, note that an additional entry was added to Info.plist to enforce this.

Info Plist

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ人海孤独症 2024-12-14 20:06:38

我过去曾遇到过这个问题。我首先“忽略”其他应用程序,然后将其设为关键窗口,从而解决了这个问题。尝试一下,看看它是否适合您。

[NSApp activateIgnoringOtherApps:YES];
[[self window]makeKeyAndOrderFront:self];

另外,请尝试在初始化期间将窗口级别设置为 NSPopUpMenuWindowLevel

[[self window]setLevel:NSPopUpMenuWindowLevel];

我在 Mac OS X 上加载 nib 文件的方式也遇到了问题。它们是“延迟”加载的,这意味着它们在需要时不会被初始化。当您想要在窗口上设置细节时,这会导致问题,但您却不能这样做,因为由于延迟笔尖加载,似乎没有调用 awakeFromNib 。为了解决这个问题,这是我过去所做的。在您的委托中(或初始化窗口的任何位置),通过访问初始化类上的 window 属性来启动窗口:

wc = [[blah alloc]initWithWindowNibName:NSStringFromClass([blah class])];
(void)[wc window]; //just kicks the lazy nib loading into gear

通过这样做,您将强制笔尖初始化。这样,当用户单击菜单栏图标时,笔尖已经初始化,并且 awakeFromNib 已经被调用。

I've had problems with this in the past. I was able to resolve it by first "ignoring" other apps, then making it the key window. Give it a shot and see if it works for you.

[NSApp activateIgnoringOtherApps:YES];
[[self window]makeKeyAndOrderFront:self];

Also, try setting the window level to NSPopUpMenuWindowLevel during initialization.

[[self window]setLevel:NSPopUpMenuWindowLevel];

I have also had problems with the way that nib files are loaded on Mac OS X. They're loaded "lazily", which means that they won't be initialized until they're needed. This causes a problem when you're wanting to set specifics on the window, but you can't because awakeFromNib doesn't seem to be called, due to lazy nib loading. To fix this, here's what I've done in the past. In your delegate (or wherever you initialize your window), kick the window into action by accessing the window property on the initialized class:

wc = [[blah alloc]initWithWindowNibName:NSStringFromClass([blah class])];
(void)[wc window]; //just kicks the lazy nib loading into gear

By doing so, you're forcing the nib to initialize. That way, when the user clicks the menubar icon the nib is already initialized, and awakeFromNib has already been called.

度的依靠╰つ 2024-12-14 20:06:38

虽然出现轻微的阴影,但面板却没有。

您是说 makeKeyAndOrderFront: 在这个 NSPanel 对象上运行您的应用程序时不显示它吗?我刚刚创建了一个示例项目,NSButton 触发相同类型的 NSPanel 来显示,并且它工作正常.. titleBar 是否启用。

http://cl.ly/3d0U3C0P3u2D0m3T1w1N

While a slight shadow appears, the panel does not.

Are you saying makeKeyAndOrderFront: on this NSPanel object doesn't display it when running your app? I just created a sample project, NSButton triggers the same type of NSPanel to display, and it works fine.. titleBar enabled or not.

http://cl.ly/3d0U3C0P3u2D0m3T1w1N

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文