How to auto hide menu bar

How to auto hide menu bar of the finder with Swift and MacOS?


I can see the Apple documentation https://developer.apple.com/documentation/appkit/nsapplication/presentationoptions/1428541-autohidemenubar


But I do not understand how to apply that. I would appreciate any orientation.


(By the way I know how to hide and show but what I am looling for is auto hide and only show when the user approach the cursor)

Answered by Claude31 in 390183022

- If I have a button to autoHideMenuBar and another button to autoHideDock it seems they are not compatible. I looked at documentation and they say "If you specify autoHideMenuBar, it must be accompanied by either hideDock or autoHideDock" I should put both in the same button? If so, I do not know how to do that.


options are defined as a set.


    @IBAction func showHide(_ sender: NSButton) {
        let application = NSApplication.shared
        let autoHidden = application.presentationOptions.contains(.autoHideMenuBar)
        if autoHidden {
            application.presentationOptions = []
        } else {
            application.presentationOptions = [.autoHideDock, .autoHideMenuBar]
        }
    }


- After I autoHideMenuBar, If I click outside the app the bar appears again. I would like to auto hide the menu bar and the dock always, not only when the user is in the app. (I would also like to let the user show both if he clicks another button)


That's normal. You control autoHide for your app only, it is not your app decision to hide manuBar for others !

To unhide, simply call:

           NSApplication.shared.presentationOptions = []

In applicationDidFinishLaunching, call:


func applicationDidFinishLaunching(_ aNotification: Notification) {

        let application = NSApplication.shared
        application.presentationOptions = .autoHideMenuBar
}

Thank you very much Claude31. It works but I have some problems:

- If I have a button to autoHideMenuBar and another button to autoHideDock it seems they are not compatible. I looked at documentation and they say "If you specify autoHideMenuBar, it must be accompanied by either hideDock or autoHideDock" I should put both in the same button? If so, I do not know how to do that.


- After I autoHideMenuBar, If I click outside the app the bar appears again. I would like to auto hide the menu bar and the dock always, not only when the user is in the app. (I would also like to let the user show both if he clicks another button)

Accepted Answer

- If I have a button to autoHideMenuBar and another button to autoHideDock it seems they are not compatible. I looked at documentation and they say "If you specify autoHideMenuBar, it must be accompanied by either hideDock or autoHideDock" I should put both in the same button? If so, I do not know how to do that.


options are defined as a set.


    @IBAction func showHide(_ sender: NSButton) {
        let application = NSApplication.shared
        let autoHidden = application.presentationOptions.contains(.autoHideMenuBar)
        if autoHidden {
            application.presentationOptions = []
        } else {
            application.presentationOptions = [.autoHideDock, .autoHideMenuBar]
        }
    }


- After I autoHideMenuBar, If I click outside the app the bar appears again. I would like to auto hide the menu bar and the dock always, not only when the user is in the app. (I would also like to let the user show both if he clicks another button)


That's normal. You control autoHide for your app only, it is not your app decision to hide manuBar for others !

To unhide, simply call:

           NSApplication.shared.presentationOptions = []

In System Preferences / General there is the option Automatically hide and show the menu bar. That is general for the mac. I know how to control that with AppleScript. If I understand your answer that is not possible with Swift. Is that right?

You can do it as a system preference (that is a user choice that he imposes to all his apps).


But AFAIK, you cannot let an app change the system behavior for other apps from within your app (which would really be confusing for user to have it done in his back).


Read this:

https://stackoverflow.com/questions/1551453/auto-hide-the-os-x-menu-bar-system-wide

How to auto hide menu bar
 
 
Q