12

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.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Samuel
  • 18,286
  • 18
  • 52
  • 88

5 Answers5

19

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 ;)

Ross
  • 14,266
  • 12
  • 60
  • 91
13

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

Avi
  • 573
  • 4
  • 4
13

As per @Ross's answer:

NSApplication.sharedApplication().mainMenu?.menuBarHeight

Unfortunately, this will return nil before the app finishes launching (because the mainMenu will 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:

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.

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
Jon
  • 74
  • 1
  • 9
Milos
  • 2,728
  • 22
  • 24
11

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

  • 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

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

(sןǝxıd oʍʇ-ʎʇuǝʍʇ :ɹǝʍsuɐ)

(ɹns ƃᴉq soɔɐɯ ǝɔuᴉs slǝxᴉd ǝʌᴉɟ-ʎʇuǝʍʇ :ǝʇɐpdn)

tmslnz
  • 1,801
  • 2
  • 15
  • 24
4

Quick and simple Swift 5 solution:

NSStatusBar.system.thickness
inexcitus
  • 2,471
  • 2
  • 26
  • 41