-2

I have a SwiftUI app and want to ensure that the window is always on top of other windows, that includes being the top layer of all my app's windows, as well as any other active / fullscreen apps running on the machine.

I have seen various similar questions with answer such as those found here

I have had some success using:

for window in NSApplication.shared.windows {
    window.level = .floating
}

Which works so long as no other app grabs full screen - however my intention now is to have it also stay on top of other full screen active apps.

Can somebody help me understand how I can access my view's class so I can override this function from a Swift Mac OS app.

RenegadeAndy
  • 5,440
  • 18
  • 70
  • 130
  • Also, `viewDidAppear()` doesn't apply here because that is a method of UIViewController, which is a UIKit class. – adamjansch Aug 18 '23 at 11:30
  • Aha - so I need an alternative approach! Slighltly updated my question with your extra info. – RenegadeAndy Aug 18 '23 at 11:30
  • 1
    https://devblogs.microsoft.com/oldnewthing/20110310-00/?p=11253 <= about Windows but informative none the less. – Mat Aug 18 '23 at 11:45
  • Interesting blog @Mat but this is partially possible already in Swift - I am just trying to adjust examples which are not suitable for my specific application - whilst paying respect to Raymond! – RenegadeAndy Aug 18 '23 at 11:55
  • SwiftUI doesn't use classes like this. However, you can access the underlying NSWindow by doing something like suggested here: https://stackoverflow.com/questions/63432700/how-to-access-nswindow-from-main-app-using-only-swiftui/63439982#63439982 – jnpdx Aug 18 '23 at 12:04
  • If you want to stay on top of full screen apps, you must set your app's activationPolicy to `accessory` and your `NSWindow` should have `canJoinAllSpaces` and `canJoinAllApplications` in the collection behavior. – jnpdx Aug 18 '23 at 13:26
  • @jnpdx can you please create an answer with code snippets on how to access the window, and assign these settings to the collection behaviour? – RenegadeAndy Aug 18 '23 at 14:28

0 Answers0