I'm loading a preferences window in the AppDelegate of my new macOS/SwiftUI app.
Callling NSRunningApplication.current.activate(options: [.activateIgnoringOtherApps, .activateAllWindows])
brings the window to the foreground to the user can see it but when I cmd-tab to switch applications, my app is the last one in the list.
Here's my code:
@objc func preferences() {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView().environmentObject(state)
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable],
backing: .buffered, defer: false)
window.isReleasedWhenClosed = false
window.center()
window.setFrameAutosaveName("Arch OS System Preferences")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
NSApp.setActivationPolicy(.regular)
NSRunningApplication.current.activate(options: [.activateIgnoringOtherApps, .activateAllWindows])
window.makeKeyAndOrderFront(nil) // tried this, didn't work
}
How can I ensure that my app is first in that list of icons?
Thanks!
question from:
https://stackoverflow.com/questions/65617184/swiftui-macos-app-nsrunningapplication-current-activate-called-but-icon-still 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…