Search results for

“swiftui”

17,491 results found

Post

Replies

Boosts

Views

Activity

Reply to Pass data to an @Observable model
@ChrisDE Questions Should CarDetailViewModel be created in CarDetailView? Or should CarDetailViewModel be created in ContentView and passed to CarDetailView? Code ContentView struct ContentView: View { @State private var selectedCarID: UUID? var body: some View { NavigationSplitView { CarListView(selectedCarID: $selectedCarID) } detail: { if let selectedCarID { CarDetailView(carID: selectedCarID) } } } } CarListViewModel @Observable class CarListViewModel { let cars = [ Car(id: UUID(), name: aaa), Car(id: UUID(), name: bbb) ] } CarListView import SwiftUI struct CarListView: View { @Binding var selectedCarID: UUID? @State var model = CarListViewModel() var body: some View { List(model.cars, selection: $selectedCarID) { car in VStack(alignment: .leading) { Text(car.name) Text((car.id)) } } } } CarDetailViewModel @Observable class CarDetailViewModel { let carID: UUID init(carID: UUID) { self.carID = carID } } CarDetailView import SwiftUI struct CarDetailView: View { @State private var model: Ca
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’26
Game Mode never engages for my card game
Environment: macOS 26.5, M4 Pro, native SwiftUI app (no MTKView/game render loop; it does use SwiftUI Metal shader effects). There's a related scheduling puzzle underneath: my app's worker threads are confined to E-cores for work that runs on P-cores from a CLI. The game computes AI decisions with brief CPU-bound bursts of parallel tree search. The identical library, doing node-count-identical work, runs ~6× slower inside the app than from a CLI on the same machine — the difference is core placement, not the workload. And since several bot decisions run in sequence each hand, that 6× stacks into user-visible seconds at the table: CLI process, default QoS: ~54ms per decision (P-cores) GUI app, worker threads at .userInitiated and .userInteractive: ~350ms (E-cores) Reproducible from the CLI with taskpolicy clamps: utility ≈ 92ms, background ≈ 584ms The app declares LSApplicationCategoryType = public.app-category.card-games and GCSupportsGameMode = true, and runs native full screen as the front
2
0
423
Jul ’26
toolbar` bottomBar disappears after rotating iPhone from portrait to landscape
I have a SwiftUI detail view with a native toolbar. On iPhone, the bottom toolbar appears correctly in portrait. After rotating the device to landscape, the bottom toolbar disappears. It does not come back unless the detail view is rebuilt. I would like to keep the native toolbar appearance and behavior, especially the iOS toolbar/glass effect. I do not want to replace it with a custom safeAreaInset bar. Environment Platform: iOS Current target/system: iOS 27 UI framework: SwiftUI Device idiom: iPhone The issue happens when rotating from portrait to landscape. Expected behavior The native bottom toolbar remains visible after device rotation. Actual behavior The native bottom toolbar is visible in portrait, but disappears after rotating to landscape. Core code The main view attaches toolbar content like this: private var contentWithToolbarAndSheets: some View { coreLayout .slateNavigationBarTitleDisplayModeInline() .toolbar { #if os(iOS) ToolbarItem(placement: .principal) { VStack(spacing: 0)
Topic: UI Frameworks SubTopic: SwiftUI
0
0
133
Jul ’26
Reply to How to style SwiftUI sidebar row selections like native macOS apps (Finder, Photos)
Hello @Moretheus, You have a good start! While I can't confirm exactly how Apple achieved a certain goal, I can show you how you can get the same result in your starting project. What you are noticing is that NSOutlineView.style = .sourceList displays the selection highlight using the system accent color. To set your own color, first disable the built-selection highlight by setting outline.selectionHighlightStyle = .none in makeNSView. Then create a separate view for the row that you can style to your liking. I used drawBackground to paint the selection and interiorBackgroundStyle to disable the SF symbol inverting to white when selected. final class SidebarRowView: NSTableRowView { override var isSelected: Bool { didSet { needsDisplay = true } } override func drawBackground(in dirtyRect: NSRect) { if isSelected { NSColor.systemGray .withAlphaComponent(0.2) .setFill() NSBezierPath(roundedRect: bounds.insetBy(dx: 8, dy: 1), xRadius: 5, yRadius: 5).fill() } } override var interiorBackgroundStyle: NSView.Backgro
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’26
Sign In with Apple: AuthorizationError 1000, empty userInfo, delegate never receives authorization
Subject: Sign In with Apple consistently fails with AuthorizationError 1000 (empty userInfo) immediately after successful biometric authentication — native delegate logging confirms failure occurs before authorization is returned to app App ID: com.andresalecina.vant
Team ID: C6GK7Z5GY2
Services ID: com.andresalecina.vant.signin Summary:
Sign In with Apple fails 100% of the time in our native iOS app (Capacitor 7 wrapper) with error code 1000 and completely empty userInfo. The native authentication sheet displays correctly and the user completes Face ID successfully, but authentication fails immediately afterward. Critical new evidence (delegate-level logging):
We instrumented both ASAuthorizationControllerDelegate callback methods directly with NSLog to determine exactly where the failure occurs: authorizationController(controller:didCompleteWithAuthorization:) — never fires. Confirmed across multiple fresh test attempts. authorizationController(controller:didCompleteWithError:) — fires every time, receiving
1
0
505
Jul ’26
How to style SwiftUI sidebar row selections like native macOS apps (Finder, Photos)
https://gist.github.com/MorusPatre/4b1e93973c3e4133794512fd7eefee48 This Is a Test App to find out how to actually achieve the exact sidebar styling Apple uses for Finder, Photos etc. The crucial part is how do I make it so the symbol and name of the selected row use the accent colour with active and inactive styling rather than having the accent colour for the row background? It shouldn't be that complicated I feel like but every AI model (even Claude Fable 5) fails at that and I haven't found apps or videos where that is explained so is that just a classic case of Apple doesn't want you to know?
2
0
382
Jul ’26
Reply to NSApp.activate() does not work with menu bar (background) apps
@Etresoft In my case, changing the activation policy doesn't work. See the video below. I even added extra DispatchQueue.main.async and switched the window from SwiftUI to AppKit - in both cases the same result. https://github.com/user-attachments/assets/6734c215-8129-4402-9f0a-b777d27195ad Nevertheless, showing a dock icon is too disruptive for menu bar apps in most cases. If we must use a workaround, I've found a better one - calling a deeplink activates the app. It just requires registering URL scheme and calling something like: NSWorkspace.shared.open(URL(string: menubar://main)!)
Topic: UI Frameworks SubTopic: AppKit Tags:
Jul ’26
Reply to NSApp.activate() does not work with menu bar (background) apps
Don't use the comments. They hide your replies. I didn't test your app. It's SwiftUI. I had looked at this recently and wanted to see if there was different behaviour in AppKit. I did not test it incorrectly. I wrote a menubar app with LSUIElement set. It behaves as you describe. But if I temporarily set the activation policy to be regular, before displaying the window, then the window appears normally. When closing the window, I could set the activation policy back to accessory. That would definitively fix the problem, at the cost of having the app temporarily appear in the dock.
Topic: UI Frameworks SubTopic: AppKit Tags:
Jul ’26
Reply to Xcode 27 How do we declare the accent color?
Here's what I've just tried: Create a new project via File > New > Project and select App as the type. Add a new asset catalog to the project using File > New > File From Template, and select Asset Catalog. Add a new ColorSet and rename it to AccentColor Reference Color.accentColor in a SwiftUI .foregroundStyle view modifier. The color in the asset catalog appears correctly. If you could provide a sample project in a feedback, that would help the investigation. Thanks!
Jul ’26
Why does SwiftUI not come with an Environment entry for the current NavigationPath?
If I have a detail view, it can include a NavigationLink (with either a specific destination view, or a value to be resolved by the nearest .navigationDestination handler in to a view). But some operations cannot be captured by a NavigationLink. For example, perhaps this detail view is editing a list, and I have a button to insert a new item. I may wish to insert the new item, and then navigate to a further editor for the inserted item. I can't do that with a NavigationLink: NavigationLink { // 🚨 This is a @ViewBuilder/@ContentBuilder, and there are no guarantees about how often it is invoked. let newItem = dataSource.insertNewItem() ItemEditor(newItem) } label: { // ... } This is the use-case which NavigationPath or typed navigation stacks handle for us: Button { let newItem = dataSource.insertNewItem() navigationPath.append(newItem) } label: { // ... } .navigationDestination(for: Item.self) { item in ItemEditor(item) } Unfortunately, while NavigationLink is able to inspect the View's environment, find the
Topic: UI Frameworks SubTopic: SwiftUI
0
0
135
Jul ’26
Reply to SwiftUI on macOS equivalent of NSSavePanel for choosing a destination URL?
@DTS Engineer We used to routinely present dialogs modally before sheets were introduced around 10.6 I believe. The introduction of sheets provided a more refined user experience and were quickly adopted by both document-based applications and shoebox-based applications. As far as I know (the HIG no longer explicitly calls this out), using sheets for open and save dialogs remains the recommended user experience. panel.runModal() has its use-cases, like when selecting a document to open after the application has launched, but it doesn't seem like the right fit for many macOS use cases. If a SwiftUI-native way to request a save destination would fit your app better, that is a good thing to send through Feedback Assistant Fair enough. Given SwiftUI already has considerable APIs for fileImporter, fileExporter, fileMover and fileDialog, I assumed I was overlooking an existing API for this common macOS use case. Thanks for the discussion.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jul ’26
Reply to NSApp.activate() does not work with menu bar (background) apps
Hi, thank you for the quick reply! I wanted to call out a specific user experience issue with this approach regarding menu bar apps. Currently, if a user clicks an item in the menu bar expecting a window to open, that window appears behind the currently active window. Because it's hidden from view, the user might assume the app is broken or didn't respond. It's hard to imagine that this is the intended behavior. Bringing the newly opened window to the foreground would be the expected behavior in this case. It seems like the new NSApp.activate() was designed for regular apps and did not take menu bar apps into account. This behavior is the same when using NSApp.activate() and SwiftUI openWindow. For now, all menu bar apps still rely on the deprecated NSApp.activate(ignoringOtherApps:), but removing this function would result in breaking thousands of apps. That's why I think there should be an alternative to NSApp.activate(ignoringOtherApps:) or the behavior of NSApp.activate() should be changed when t
Topic: UI Frameworks SubTopic: AppKit Tags:
Jul ’26
Reply to NSApp.activate() does not work with menu bar (background) apps
Thank you for the post. Very interesting post. The strictness of the new window activation model is intended, I believe the goal was to stop apps from aggressively stealing focus from the user while they are typing or working in another app. Because of this, NSApp.activate(ignoringOtherApps:) was deprecated. https://developer.apple.com/documentation/appkit/nsapplication/activate(ignoringotherapps:) You should use https://developer.apple.com/documentation/appkit/nsapplication/activate() If you are using SwiftUI lifecycle with MenuBarExtra and WindowGroup, relying on the environment's openWindow to handles the underlying activation better than bridging to AppKit: @Environment(.openWindow) var openWindow // Inside your MenuBarExtra... Button(Open) { openWindow(id: my-main-window) } My code is for reference only as I don't validate it with Xcode. If the behavior deviates from your expectations, the bug filed as an enhancement will be forwarded to the relevant team for evaluation of your requirements. The
Topic: UI Frameworks SubTopic: AppKit Tags:
Jul ’26
Reply to SwiftUI on macOS equivalent of NSSavePanel for choosing a destination URL?
Thanks for writing back. You may already be doing this, in which case take it as confirmation rather than news: NSSavePanel does not need an NSWindow to present at all. Calling panel.runModal() from your SwiftUI action presents the panel app-modally and returns the chosen URL, with no window reference involved. That keeps you entirely within the SwiftUI app lifecycle, and it avoids the window question entirely. The tradeoff is that the panel appears as a standalone modal dialog rather than as a sheet attached to your window. The window only matters if you specifically want that document-modal sheet, through beginSheetModal(for:). That path does need the hosting NSWindow, and as of macOS 26 SwiftUI does not expose a first-class accessor for it. If your app has a single window, NSApp.keyWindow is a perfectly good way to get it. The ambiguity only arises once more than one window can be key; for that case, a small NSViewRepresentable that reads its view's window is the more robust idio
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’26
Reply to Pass data to an @Observable model
@ChrisDE Questions Should CarDetailViewModel be created in CarDetailView? Or should CarDetailViewModel be created in ContentView and passed to CarDetailView? Code ContentView struct ContentView: View { @State private var selectedCarID: UUID? var body: some View { NavigationSplitView { CarListView(selectedCarID: $selectedCarID) } detail: { if let selectedCarID { CarDetailView(carID: selectedCarID) } } } } CarListViewModel @Observable class CarListViewModel { let cars = [ Car(id: UUID(), name: aaa), Car(id: UUID(), name: bbb) ] } CarListView import SwiftUI struct CarListView: View { @Binding var selectedCarID: UUID? @State var model = CarListViewModel() var body: some View { List(model.cars, selection: $selectedCarID) { car in VStack(alignment: .leading) { Text(car.name) Text((car.id)) } } } } CarDetailViewModel @Observable class CarDetailViewModel { let carID: UUID init(carID: UUID) { self.carID = carID } } CarDetailView import SwiftUI struct CarDetailView: View { @State private var model: Ca
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’26
Game Mode never engages for my card game
Environment: macOS 26.5, M4 Pro, native SwiftUI app (no MTKView/game render loop; it does use SwiftUI Metal shader effects). There's a related scheduling puzzle underneath: my app's worker threads are confined to E-cores for work that runs on P-cores from a CLI. The game computes AI decisions with brief CPU-bound bursts of parallel tree search. The identical library, doing node-count-identical work, runs ~6× slower inside the app than from a CLI on the same machine — the difference is core placement, not the workload. And since several bot decisions run in sequence each hand, that 6× stacks into user-visible seconds at the table: CLI process, default QoS: ~54ms per decision (P-cores) GUI app, worker threads at .userInitiated and .userInteractive: ~350ms (E-cores) Reproducible from the CLI with taskpolicy clamps: utility ≈ 92ms, background ≈ 584ms The app declares LSApplicationCategoryType = public.app-category.card-games and GCSupportsGameMode = true, and runs native full screen as the front
Replies
2
Boosts
0
Views
423
Activity
Jul ’26
toolbar` bottomBar disappears after rotating iPhone from portrait to landscape
I have a SwiftUI detail view with a native toolbar. On iPhone, the bottom toolbar appears correctly in portrait. After rotating the device to landscape, the bottom toolbar disappears. It does not come back unless the detail view is rebuilt. I would like to keep the native toolbar appearance and behavior, especially the iOS toolbar/glass effect. I do not want to replace it with a custom safeAreaInset bar. Environment Platform: iOS Current target/system: iOS 27 UI framework: SwiftUI Device idiom: iPhone The issue happens when rotating from portrait to landscape. Expected behavior The native bottom toolbar remains visible after device rotation. Actual behavior The native bottom toolbar is visible in portrait, but disappears after rotating to landscape. Core code The main view attaches toolbar content like this: private var contentWithToolbarAndSheets: some View { coreLayout .slateNavigationBarTitleDisplayModeInline() .toolbar { #if os(iOS) ToolbarItem(placement: .principal) { VStack(spacing: 0)
Topic: UI Frameworks SubTopic: SwiftUI
Replies
0
Boosts
0
Views
133
Activity
Jul ’26
Does SwiftUI pass views via the preference and environment, if so, how?
I am under the assumption that SwiftUI does pass views via preference atleast (navigationDestination, toolbar, sheet, alerts, popover etc.,). How does it maintain view identity and does it wrap the passing view in AnyView or internally it uses type inference magic, that is not available to us?. If it does not, then how does it pass them instead ?
Replies
0
Boosts
0
Views
182
Activity
Jul ’26
Reply to How to style SwiftUI sidebar row selections like native macOS apps (Finder, Photos)
Hello @Moretheus, You have a good start! While I can't confirm exactly how Apple achieved a certain goal, I can show you how you can get the same result in your starting project. What you are noticing is that NSOutlineView.style = .sourceList displays the selection highlight using the system accent color. To set your own color, first disable the built-selection highlight by setting outline.selectionHighlightStyle = .none in makeNSView. Then create a separate view for the row that you can style to your liking. I used drawBackground to paint the selection and interiorBackgroundStyle to disable the SF symbol inverting to white when selected. final class SidebarRowView: NSTableRowView { override var isSelected: Bool { didSet { needsDisplay = true } } override func drawBackground(in dirtyRect: NSRect) { if isSelected { NSColor.systemGray .withAlphaComponent(0.2) .setFill() NSBezierPath(roundedRect: bounds.insetBy(dx: 8, dy: 1), xRadius: 5, yRadius: 5).fill() } } override var interiorBackgroundStyle: NSView.Backgro
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’26
Sign In with Apple: AuthorizationError 1000, empty userInfo, delegate never receives authorization
Subject: Sign In with Apple consistently fails with AuthorizationError 1000 (empty userInfo) immediately after successful biometric authentication — native delegate logging confirms failure occurs before authorization is returned to app App ID: com.andresalecina.vant
Team ID: C6GK7Z5GY2
Services ID: com.andresalecina.vant.signin Summary:
Sign In with Apple fails 100% of the time in our native iOS app (Capacitor 7 wrapper) with error code 1000 and completely empty userInfo. The native authentication sheet displays correctly and the user completes Face ID successfully, but authentication fails immediately afterward. Critical new evidence (delegate-level logging):
We instrumented both ASAuthorizationControllerDelegate callback methods directly with NSLog to determine exactly where the failure occurs: authorizationController(controller:didCompleteWithAuthorization:) — never fires. Confirmed across multiple fresh test attempts. authorizationController(controller:didCompleteWithError:) — fires every time, receiving
Replies
1
Boosts
0
Views
505
Activity
Jul ’26
How to style SwiftUI sidebar row selections like native macOS apps (Finder, Photos)
https://gist.github.com/MorusPatre/4b1e93973c3e4133794512fd7eefee48 This Is a Test App to find out how to actually achieve the exact sidebar styling Apple uses for Finder, Photos etc. The crucial part is how do I make it so the symbol and name of the selected row use the accent colour with active and inactive styling rather than having the accent colour for the row background? It shouldn't be that complicated I feel like but every AI model (even Claude Fable 5) fails at that and I haven't found apps or videos where that is explained so is that just a classic case of Apple doesn't want you to know?
Replies
2
Boosts
0
Views
382
Activity
Jul ’26
Reply to NSApp.activate() does not work with menu bar (background) apps
@Etresoft In my case, changing the activation policy doesn't work. See the video below. I even added extra DispatchQueue.main.async and switched the window from SwiftUI to AppKit - in both cases the same result. https://github.com/user-attachments/assets/6734c215-8129-4402-9f0a-b777d27195ad Nevertheless, showing a dock icon is too disruptive for menu bar apps in most cases. If we must use a workaround, I've found a better one - calling a deeplink activates the app. It just requires registering URL scheme and calling something like: NSWorkspace.shared.open(URL(string: menubar://main)!)
Topic: UI Frameworks SubTopic: AppKit Tags:
Replies
Boosts
Views
Activity
Jul ’26
Reply to NSApp.activate() does not work with menu bar (background) apps
Don't use the comments. They hide your replies. I didn't test your app. It's SwiftUI. I had looked at this recently and wanted to see if there was different behaviour in AppKit. I did not test it incorrectly. I wrote a menubar app with LSUIElement set. It behaves as you describe. But if I temporarily set the activation policy to be regular, before displaying the window, then the window appears normally. When closing the window, I could set the activation policy back to accessory. That would definitively fix the problem, at the cost of having the app temporarily appear in the dock.
Topic: UI Frameworks SubTopic: AppKit Tags:
Replies
Boosts
Views
Activity
Jul ’26
Reply to Xcode 27 How do we declare the accent color?
Here's what I've just tried: Create a new project via File > New > Project and select App as the type. Add a new asset catalog to the project using File > New > File From Template, and select Asset Catalog. Add a new ColorSet and rename it to AccentColor Reference Color.accentColor in a SwiftUI .foregroundStyle view modifier. The color in the asset catalog appears correctly. If you could provide a sample project in a feedback, that would help the investigation. Thanks!
Replies
Boosts
Views
Activity
Jul ’26
Why does SwiftUI not come with an Environment entry for the current NavigationPath?
If I have a detail view, it can include a NavigationLink (with either a specific destination view, or a value to be resolved by the nearest .navigationDestination handler in to a view). But some operations cannot be captured by a NavigationLink. For example, perhaps this detail view is editing a list, and I have a button to insert a new item. I may wish to insert the new item, and then navigate to a further editor for the inserted item. I can't do that with a NavigationLink: NavigationLink { // 🚨 This is a @ViewBuilder/@ContentBuilder, and there are no guarantees about how often it is invoked. let newItem = dataSource.insertNewItem() ItemEditor(newItem) } label: { // ... } This is the use-case which NavigationPath or typed navigation stacks handle for us: Button { let newItem = dataSource.insertNewItem() navigationPath.append(newItem) } label: { // ... } .navigationDestination(for: Item.self) { item in ItemEditor(item) } Unfortunately, while NavigationLink is able to inspect the View's environment, find the
Topic: UI Frameworks SubTopic: SwiftUI
Replies
0
Boosts
0
Views
135
Activity
Jul ’26
Reply to SwiftUI on macOS equivalent of NSSavePanel for choosing a destination URL?
@DTS Engineer We used to routinely present dialogs modally before sheets were introduced around 10.6 I believe. The introduction of sheets provided a more refined user experience and were quickly adopted by both document-based applications and shoebox-based applications. As far as I know (the HIG no longer explicitly calls this out), using sheets for open and save dialogs remains the recommended user experience. panel.runModal() has its use-cases, like when selecting a document to open after the application has launched, but it doesn't seem like the right fit for many macOS use cases. If a SwiftUI-native way to request a save destination would fit your app better, that is a good thing to send through Feedback Assistant Fair enough. Given SwiftUI already has considerable APIs for fileImporter, fileExporter, fileMover and fileDialog, I assumed I was overlooking an existing API for this common macOS use case. Thanks for the discussion.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jul ’26
Reply to NSApp.activate() does not work with menu bar (background) apps
Hi, thank you for the quick reply! I wanted to call out a specific user experience issue with this approach regarding menu bar apps. Currently, if a user clicks an item in the menu bar expecting a window to open, that window appears behind the currently active window. Because it's hidden from view, the user might assume the app is broken or didn't respond. It's hard to imagine that this is the intended behavior. Bringing the newly opened window to the foreground would be the expected behavior in this case. It seems like the new NSApp.activate() was designed for regular apps and did not take menu bar apps into account. This behavior is the same when using NSApp.activate() and SwiftUI openWindow. For now, all menu bar apps still rely on the deprecated NSApp.activate(ignoringOtherApps:), but removing this function would result in breaking thousands of apps. That's why I think there should be an alternative to NSApp.activate(ignoringOtherApps:) or the behavior of NSApp.activate() should be changed when t
Topic: UI Frameworks SubTopic: AppKit Tags:
Replies
Boosts
Views
Activity
Jul ’26
Reply to NSApp.activate() does not work with menu bar (background) apps
Thank you for the post. Very interesting post. The strictness of the new window activation model is intended, I believe the goal was to stop apps from aggressively stealing focus from the user while they are typing or working in another app. Because of this, NSApp.activate(ignoringOtherApps:) was deprecated. https://developer.apple.com/documentation/appkit/nsapplication/activate(ignoringotherapps:) You should use https://developer.apple.com/documentation/appkit/nsapplication/activate() If you are using SwiftUI lifecycle with MenuBarExtra and WindowGroup, relying on the environment's openWindow to handles the underlying activation better than bridging to AppKit: @Environment(.openWindow) var openWindow // Inside your MenuBarExtra... Button(Open) { openWindow(id: my-main-window) } My code is for reference only as I don't validate it with Xcode. If the behavior deviates from your expectations, the bug filed as an enhancement will be forwarded to the relevant team for evaluation of your requirements. The
Topic: UI Frameworks SubTopic: AppKit Tags:
Replies
Boosts
Views
Activity
Jul ’26
Reply to SwiftUI on macOS equivalent of NSSavePanel for choosing a destination URL?
Thanks for writing back. You may already be doing this, in which case take it as confirmation rather than news: NSSavePanel does not need an NSWindow to present at all. Calling panel.runModal() from your SwiftUI action presents the panel app-modally and returns the chosen URL, with no window reference involved. That keeps you entirely within the SwiftUI app lifecycle, and it avoids the window question entirely. The tradeoff is that the panel appears as a standalone modal dialog rather than as a sheet attached to your window. The window only matters if you specifically want that document-modal sheet, through beginSheetModal(for:). That path does need the hosting NSWindow, and as of macOS 26 SwiftUI does not expose a first-class accessor for it. If your app has a single window, NSApp.keyWindow is a perfectly good way to get it. The ambiguity only arises once more than one window can be key; for that case, a small NSViewRepresentable that reads its view's window is the more robust idio
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’26