NSWindow-Levels
A small example on using NSWindow.Level in SwiftUI on MacOS
Instructions
- Download & compile
- Run!
- To change levels, select one and refresh!
- To visit Apple’s documentation, press the “documentation” button!
Do It Yourself!
Maybe you don’t want to download the project. That’s fine! Here’s a minimal example:
func setLevel(level: NSWindow.Level) {
for window in NSApplication.shared.windows {
window.level = level
}
}
This is a function that you can put in the app you’re making. The available values for the “level” variable in the function are:
.normal
.floating
.submenu
.tornOffMenu
.mainMenu
.statusBar
.modalPanel
.popUpMenu
.screenSaver
Additionally, you can use NSWindow.Level(rawValue: NSWindow.Level.normal.rawValue - 1)
to make the window always stay below other windows.
Sources
Thank you very much to those who made/answered these sources!
- https://developer.apple.com/documentation/appkit/nswindow/level
- https://www.reddit.com/r/swift/comments/ccyi9e/make_window_always_on_bottom_macos/
- https://stackoverflow.com/questions/66619671/change-macos-window-level-with-swiftui-to-make-a-floating-window