macos 苹果菜单栏的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2867503/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:57:14  来源:igfitidea点击:

Height of the apple menubar

macosmenubarosx-leopard

提问by Samuel

I was just wondering how to get the height of the apple menubar, in pixels (the one always on top)

我只是想知道如何获得苹果菜单栏的高度,以像素为单位(总是在顶部)

(My screen size is 1200 x 800) i was wondering what it would be excluding the menubar.

(我的屏幕尺寸是 1200 x 800)我想知道不包括菜单栏会是什么。

回答by Ross

Programmatically (as this is SO, I was looking for a programmatical answer and google sent me here) you can get this by using:

以编程方式(因为这是这样,我一直在寻找编程方式的答案,谷歌将我发送到这里)您可以使用以下方法获得:

 CGFloat menuBarHeight = [[[NSApplication sharedApplication] mainMenu] menuBarHeight];

or as a graphic designer I know would say: 0.8cm ;)

或者作为我知道的平面设计师会说:0.8cm ;)

回答by Avi

And if you need to know at runtime, use -[NSMenu menuBarHeight].

如果您需要在运行时知道,请使用-[NSMenu menuBarHeight].

回答by tmslnz

The non-programmatic-6-nanoseconds way I would find out:

我会发现的非编程 6 纳秒方式:

  • Step 1: ? + ? + 4
  • Step 2: press ? (space) and your cursor will turn from a crosshair to a something that selects whole windows.
  • Step 3: CLICK
  • Step 4: ~/Desktop/Screen shot 2001-##-##.png/jpg
  • Step 5: ? + i
  • 步骤1: ?+ ? + 4
  • 第 2 步:按 ? (空格),您的光标将从十字准线变成选择整个窗口的东西。
  • 第 3 步:单击
  • 第 4 步:~/桌面/屏幕截图 2001-##-##.png/jpg
  • 第 5 步: ? +我

The info panel will tell you the height (and width).

信息面板会告诉您高度(和宽度)。

(s??x?d o??-??u??? :???su?)

(s??x?do??-??u???:???su?)

回答by milos

As per @Ross's answer:

根据@Ross 的回答:

NSApplication.sharedApplication().mainMenu?.menuBarHeight

Unfortunately, this will return nilbefore the app finishes launching (because the mainMenuwill be nil). If you need this value earlier than that (and you don't want to guess it for the future OS releases), you can calculate it like so:

不幸的是,这将nil在应用程序完成启动之前返回(因为mainMenu将是nil)。如果您在此之前需要这个值(并且您不想为未来的操作系统版本猜测它),您可以像这样计算它:

if let screen = NSScreen.mainScreen() {
    let menuBarHeight = screen.frame.height - screen.visibleFrame.height - screen.visibleFrame.origin.y - 1
}

This number will not be correct only if there is some extra screen furniture (like the Dock for example) pinned from the top, which seems extremely unlikely.

仅当有一些额外的屏幕家具(例如 Dock)从顶部固定时,这个数字才会正确,这似乎极不可能。

Update:To support multiple displays (primary and secondary):

更新:支持多个显示器(主要和次要):

let appleMenuBarHeight = screen.frame.height - screen.visibleFrame.height - (screen.visibleFrame.origin.y - screen.frame.origin.y) - 1