macos 如何更改 NSWindow 标题栏的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4639207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the height of an NSWindow titlebar?
提问by Flocked
I want to change the height of an NSWindow titlebar.
我想更改 NSWindow 标题栏的高度。
Here are some examples:
这里有些例子:
And…
和…
I could use an NSToolbar, but the problem is that I can't place views very height (For example: I can't place the segmentedControl higher than in the picture because there is still the titlebar)
我可以使用 NSToolbar,但问题是我不能将视图放置得非常高(例如:我不能将 segmentedControl 放置在高于图片中的位置,因为标题栏仍然存在)
If I remove the titlebar I can't place a NSToolbar and the window isn't movable.
如果我删除标题栏,我将无法放置 NSToolbar 并且窗口不可移动。
Have you any ideas?
你有什么想法吗?
采纳答案by damo
INAppStoreWindow is a NSWindow subclass, it tell you how to change the height of title bar.
INAppStoreWindow 是一个 NSWindow 子类,它告诉你如何改变标题栏的高度。
https://github.com/indragiek/INAppStoreWindow
https://github.com/indragiek/INAppStoreWindow
http://iloveco.de/adding-a-titlebar-accessory-view-to-a-window/
This example tells you how to add buttons in the title bar.
http://iloveco.de/adding-a-titlebar-accessory-view-to-a-window/
这个例子告诉你如何在标题栏中添加按钮。
回答by Arvin
This is much easier than one would think. I too went on a quest to do something similar for my app.
这比人们想象的要容易得多。我也继续寻求为我的应用程序做一些类似的事情。
Real App Store app:
真正的应用商店应用:
My App Store app look-alike:
我的 App Store 应用看起来很像:
No disrespect to INAppStoreWindow, it is a very good implementation and solid. The only draw back I saw from it though was that there was a lot of drawing code along with hardcoded settings for the TitleBar colors which Apple can adjust at anytime.
没有不尊重 INAppStoreWindow,它是一个非常好的实现和可靠的。我从中看到的唯一缺点是有很多绘图代码以及 Apple 可以随时调整的 TitleBar 颜色的硬编码设置。
So here is how I did it:
所以这就是我是如何做到的:
A) Create a standard window with a Title Bar, Close, Minimize, Shadow, Resize, Full Screen - Primary Window all set. Note: You do not need a textured window nor should you set a title
A) 创建一个带有标题栏、关闭、最小化、阴影、调整大小、全屏 - 主窗口的标准窗口。注意:您不需要带纹理的窗口,也不应该设置标题
B) Next add a standard toolbar with these settings:
B) 接下来添加具有以下设置的标准工具栏:
- Icon Only
- Visible at Launch - ON
- Customizable - OFF
- Separator - ON
- Size - Regular
- 仅图标
- 启动时可见 - 开启
- 可定制 - 关闭
- 分离器 - 开
- 尺寸 - 常规
Remove all the Toolbar Items and add only these in the following order
删除所有工具栏项并按以下顺序仅添加这些项
NSSegmentControl (51 x 24) -- | Flexible Space | -- NSSearchField (150 x 25)
NSSegmentControl (51 x 24) -- | 灵活空间 | -- NSSearchField (150 x 25)
C) In your content View directly under the toolbar add a regular sized NSButton set like so:
C) 在工具栏正下方的内容视图中,添加一个常规大小的 NSButton 集,如下所示:
- Bordered - OFF
- Transparent - OFF
- Title -
- Image -
- Position - Text below the button
- Font - System Small 11
- 有边框 - 关闭
- 透明 - 关闭
- 标题 -
- 图片 -
- 位置 - 按钮下方的文本
- 字体 - 系统小 11
Ok, pretty easy so far, right?!
好的,到目前为止很容易,对吧?!
In your Window Controller or app delegate.... setup IBOutlet(s) to your NSButton(s)
在您的窗口控制器或应用程序委托中.... 将 IBOutlet(s) 设置为您的 NSButton(s)
Note: Remember to hook up your IBOutlet in interface builder
注意:记得在界面生成器中连接你的 IBOutlet
Ok don't be scared we have to write a tiny bit of code now:
好的,不要害怕我们现在必须编写一点代码:
In awakeFromNib or windowDidLoad....
在awakeFromNib 或windowDidLoad....
- Get the content views' superview (aka NSThemeView)
- Remove your button from its superView
- Set the frame of your button
- Add the button back to the theme view
- 获取内容视图的超级视图(又名 NSThemeView)
- 从其超级视图中删除您的按钮
- 设置按钮的框架
- 将按钮添加回主题视图
So the code would look similar to this:
所以代码看起来类似于:
NSView *themeView = [self.contentView superview];
NSUInteger adj = 6;
[self.btnFeatured removeFromSuperview];
self.btnFeatured.frame = NSMakeRect( self.btnFeatured.frame.origin.x,
self.window.frame.size.height - self.btnFeatured.frame.size.height - adj,
self.btnFeatured.frame.size.width, self.btnFeatured.frame.size.height);
[themeView addSubview:self.btnFeatured];
That's it! You can use your outlet to enable/disable your button, setup a mask image when selected, enable/disable the toolbar or even hide everything and add a window title. All of this without worry if Apple changes their standard Window Titlebars.
而已!您可以使用插座启用/禁用您的按钮、在选择时设置蒙版图像、启用/禁用工具栏甚至隐藏所有内容并添加窗口标题。如果 Apple 更改其标准窗口标题栏,所有这些都无需担心。
P.S. No private frameworks were used in this posting whatsoever!
PS 在这篇文章中没有使用任何私有框架!
回答by Eimantas
You'd have to subclass NSWindow
and do a custom window frame drawing. It's not only about a titlebar. It's about whole window frame (so you can, actually, put close/minimize/zoom buttons at the bottom if you wish).
您必须子类化NSWindow
并进行自定义窗口框架绘图。这不仅仅是关于标题栏。它是关于整个窗口框架的(实际上,如果您愿意,您可以在底部放置关闭/最小化/缩放按钮)。
A good starter is at "Cocoa with love" website.
“Cocoa with love”网站是一个很好的开端。
回答by bo zhang
There are a few new solutions based on INAppStoreWindow and without warning and log message, for anyone who wants to change the height of NStitlebar, change the position of traffic light, add an item(e.g. a NSbutton) on NStitlebar and change its position, please check below.
有一些基于 INAppStoreWindow 的新解决方案,没有警告和日志消息,对于任何想要更改 NStitlebar 高度、更改交通灯位置、在 NStitlebar 上添加项目(例如 NSbutton)并更改其位置的人,请检查下面。
WAYWindow: https://github.com/weAreYeah/WAYWindow
WAYWindow:https: //github.com/weAreYeah/WAYWindow
NStitlebar_with_item: https://github.com/ZHANGneuro/NStitlebar_with_item
NStitlebar_with_item: https://github.com/ZHANGneuro/NStitlebar_with_item