Usable screen space in a Mac

(Swift, macOS, storyboards)

I can find the total height of the screen: let totalHight = NSScreen.main?.frame.height

To know the real space I have to subtract 23 px for the Menu Bar.

let usableHight = totalHight - 23

But on some occasions the user may not have the Menu Bar active (System Preferences > General > uncheck Automatically hide and show the menu bar)

Is there a way to know if the Menu Bar is hidden or show? or perhaps there is another way to know the real usable height of the screen?

Accepted Reply

Try the following

let usableHight = NSMenu.menuBarVisible() ? totalHight - 23 : totalHight

Here is a simple test that works:

        let totalHeight = 400
        var usableHeight = NSMenu.menuBarVisible() ? totalHeight - 23 : totalHeight
        print(usableHeight)
        NSMenu.setMenuBarVisible(false)
        usableHeight = NSMenu.menuBarVisible() ? totalHeight - 23 : totalHeight
        print(usableHeight)
  • It works very well. Thank you!

    Can you explain this line? Where can I learn more about it?: let usableHight = NSMenu.menuBarVisible() ? totalHight - 23 : totalHight I would like to learn more. I understand what it does, but I do not know what it is or how to reproduce in another circumstance.

  • this line      usableHeight = NSMenu.menuBarVisible() ? totalHeight - 23 : totalHeight      uses ternary operator ; is a condensed form of it then else.  It is equivalent to:     if NSMenu.menuBarVisible() { usableHeight = totalHeight - 23 } else { usableHeight = totalHeight }. Swift programming language explains « The ternary conditional operator is a special operator with three parts, which takes the form question ? answer1 : answer2. It is a shortcut for evaluating one of two expressions based on whether question is true or false. If question is true, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value. ». It is very convenient when you have to set a value depending on condition. For instance let color = (score < 10) ? UIColor.red : UIColor.green

  • Thank you. I love to learn new things!

Add a Comment

Replies

Did you try to use menuBarVisible ?

https://developer.apple.com/documentation/appkit/nsmenu/1518236-menubarvisible

  • I do not know how to apply this information. I am eager to learn, but I do not know how

Add a Comment

Try the following

let usableHight = NSMenu.menuBarVisible() ? totalHight - 23 : totalHight

Here is a simple test that works:

        let totalHeight = 400
        var usableHeight = NSMenu.menuBarVisible() ? totalHeight - 23 : totalHeight
        print(usableHeight)
        NSMenu.setMenuBarVisible(false)
        usableHeight = NSMenu.menuBarVisible() ? totalHeight - 23 : totalHeight
        print(usableHeight)
  • It works very well. Thank you!

    Can you explain this line? Where can I learn more about it?: let usableHight = NSMenu.menuBarVisible() ? totalHight - 23 : totalHight I would like to learn more. I understand what it does, but I do not know what it is or how to reproduce in another circumstance.

  • this line      usableHeight = NSMenu.menuBarVisible() ? totalHeight - 23 : totalHeight      uses ternary operator ; is a condensed form of it then else.  It is equivalent to:     if NSMenu.menuBarVisible() { usableHeight = totalHeight - 23 } else { usableHeight = totalHeight }. Swift programming language explains « The ternary conditional operator is a special operator with three parts, which takes the form question ? answer1 : answer2. It is a shortcut for evaluating one of two expressions based on whether question is true or false. If question is true, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value. ». It is very convenient when you have to set a value depending on condition. For instance let color = (score < 10) ? UIColor.red : UIColor.green

  • Thank you. I love to learn new things!

Add a Comment

Try the following

let usableHight = NSMenu.menuBarVisible() ? totalHight - 23 : totalHight

Here is a simple test that works:

        let totalHeight = 400
        var usableHeight = NSMenu.menuBarVisible() ? totalHeight - 23 : totalHeight
        print(usableHeight)
        NSMenu.setMenuBarVisible(false)
        usableHeight = NSMenu.menuBarVisible() ? totalHeight - 23 : totalHeight
        print(usableHeight)

yields:

  • 377
  • hides menu bar
  • 400