Provide views, controls, and layout structures for declaring your app's user interface using SwiftUI.

Posts under SwiftUI tag

200 Posts

Post

Replies

Boosts

Views

Activity

Can't get a scoped resource URL from drag and drop
Hi, My Mac app allows a customer to drag and drop a file package onto a SwiftUI view. I can't seem to find a way to successfully call .startAccessingSecurityScopedResource() with the file/dir that was dropped into the view. I put together a simple test app. Here is the code: struct ContentView: View { @State var isTargetedForDrop: Bool = false var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") Rectangle() .stroke(Color.gray) .onDrop(of: [UTType.fileURL], isTargeted: $isTargetedForDrop) { providers in guard let provider = providers.first(where: { $0.hasItemConformingToTypeIdentifier(UTType.fileURL.identifier) }) else { return false } provider.loadItem(forTypeIdentifier: UTType.fileURL.identifier, options: nil) { item, error in if let error = error { print("Drop load error: \(error)") return } if let url = item as? URL { print("Dropped file URL: \(url)") } else if let data = item as? Data, let url = URL(dataRepresentation: data, relativeTo: nil) { print("Dropped file URL (from data): \(url)") let access = url.startAccessingSecurityScopedResource() if access { print("Successfully accessed file at URL: \(url)") } else { print("Failed to access file at URL: \(url)") } url.stopAccessingSecurityScopedResource() } else { print("Unsupported dropped item: \(String(describing: item))") } } return true } } .padding() } } When I drop a file package into this view I see, "Failed to access file at URL: <the_full_file_path>" I'm running Xcode 26 on macOS 26.
1
0
51
4d
How can I render a ScrollView so that its ScrollPosition is at the correct ID during layout?
Below you will find an example view of my problem. It has one button that, when pressed, will toggle between two scroll views using withAnimation, setting the scroll position onto the 2nd and 3rd items for either scroll view in onAppear. The intent is to have the background of items within each list transition smoothly from their position in one list, to their position in the other. However, this does not appear to be easily possible when setting the list position using an ID/ScrollPosition: Initializing a ScrollPosition with the correct ID and rendering the ScrollView with that appears to have no effect - the ScrollView will be drawn at the top of the scroll contents The only way I've found to render the ScrollView at an ID position is to scroll to that position in onAppear. However, it appears that when doing so, the matchedGeometryEffect interpolates the position of the elements as if the contentOffset.y of the ScrollView is briefly 0, resulting in a strange effect The desired animation can be seen if the two lists are toggled rapidly, allowing for the matchedGeometryEffect to smooth out the brief y 0 and interpolate between the correct positions It seems I either need to a) ensure the list is laid out at the correct y location beforehand (very difficult with dynamic list items, but seems to solve this problem if setting the y position explicitly) b) ensure that the list is laid out at the correct ID beforehand (have not been able to figure out how) c) ensure the matched geometry effect animation ignores the brief "0" y offset of the ScrollView before setting the ID position in onAppear (have not been able to figure out how) Note that I have to use VStack here for the matched geometry effect to work consistently. Any ideas on solving this? Code: import SwiftUI struct Item: Identifiable { let id = UUID().uuidString var height: CGFloat var label: String } enum TestScrollListStyle { case primary case alternate } struct TestScrollList: View { let items: [Item] let style: TestScrollListStyle let namespace: Namespace.ID @Binding var scrollPosition: ScrollPosition var initialIndex: Int = 2 var body: some View { ScrollView { VStack(spacing: style == .primary ? 8 : 16) { ForEach(items, id: \.id) { item in switch style { case .primary: Text(item.label) .frame(maxWidth: .infinity) .frame(height: item.height) .padding(.horizontal) .background( Rectangle() .fill(.blue.opacity(0.15)) .matchedGeometryEffect(id: item.id, in: namespace) ) case .alternate: HStack { Circle() .fill(.green.opacity(0.25)) .frame(width: 24, height: 24) Text(item.label) .frame(maxWidth: .infinity, alignment: .leading) } .frame(height: item.height) .padding(.horizontal) .background( Rectangle() .fill(.green.opacity(0.08)) .matchedGeometryEffect(id: item.id, in: namespace) ) } } } .scrollTargetLayout() .padding(.vertical) } .scrollPosition($scrollPosition, anchor: .top) .onAppear { var tx = Transaction() tx.disablesAnimations = true withTransaction(tx) { if items.indices.contains(initialIndex) { scrollPosition.scrollTo(id: items[initialIndex].id) } } } } } struct ContentView: View { @Namespace private var matchedNamespace @State private var items: [Item] = (0..<10).map { i in Item(height: .random(in: 80...220), label: "Row \(i)") } @State private var showAlternateView: Bool = false // Scroll positions for each scroll view @State private var primaryScrollPosition = ScrollPosition(idType: String.self) @State private var alternateScrollPosition = ScrollPosition(idType: String.self) var body: some View { NavigationStack { ZStack { if !showAlternateView { TestScrollList( items: items, style: .primary, namespace: matchedNamespace, scrollPosition: $primaryScrollPosition, initialIndex: 2 ) } if showAlternateView { TestScrollList( items: items, style: .alternate, namespace: matchedNamespace, scrollPosition: $alternateScrollPosition, initialIndex: 3 ) } } .navigationTitle("Two ScrollViews + Matched Geometry") .toolbar { ToolbarItem(placement: .topBarTrailing) { Button(showAlternateView ? "Primary" : "Alternate") { withAnimation() { showAlternateView.toggle() } } } } } } } #Preview { ContentView() }
1
0
34
4d
SwiftUI Slider onEditingChanged is unreliable on iOS 26
For information I stumbled upon a regression with SwiftUI Slider on iOS 26. Its onEditingChanged closure might be called twice when interaction ends, with a final Boolean incorrect value of true provided to the closure. As a result apps cannot reliably rely on this closure to detect when an interaction with the slider starts or ends. I filed a feedback under FB20283439 (iOS 26.0 regression: Slider onEditingChanged closure is unreliable).
5
3
229
4d
Safe areas ignored after navigating a WebView/WebPage back in a NavigationStack
I'm using the new iOS 26 WebPage/WebView for SwiftUI in a NavigationStack. The initial load works as expected, but when loading items from the back/forward lists, the content jumps beneath the navigation bar: struct WebPageTestView: View { var webPage = WebPage() var body: some View { NavigationStack { WebView(webPage) .toolbar { Button("Back") { if let backItem = webPage.backForwardList.backList.last { webPage.load(backItem) } } Button("Forward") { if let forwardItem = webPage.backForwardList.forwardList.first { webPage.load(forwardItem) } } } } .task { webPage.isInspectable = true webPage.load(URL(string: "https://domchristie.co.uk/")) } } } I have run this on the iOS 26.0 and 26.1 Simulators and get the same issue. The demo website does not use any JavaScript. I was able to replicate this behaviour using a wrapped WKWebView and calling the .ignoresSafeArea(.all) modifier.
3
1
436
4d
Issue with SwiftData inheritance
Every time I insert a subclass (MYShapeLayer) into the model context, the app crashes with an error: DesignerPlayground crashed due to fatalError in BackingData.swift at line 908. Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0xb2dbc55f3f4c57f2 <x-coredata://B1E3206B-40DE-4185-BC65-4540B4705B40/MYShapeLayer/p1>))) with Optional(A6CA4F89-107F-4A66-BC49-DD7DAC689F77) struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var designs: [MYDesign] var layers: [MYLayer] { designs.first?.layers ?? [] } var body: some View { NavigationStack { List { ForEach(layers) { layer in Text(layer.description) } } .onAppear { let design = MYDesign(title: "My Design") modelContext.insert(design) try? modelContext.save() } .toolbar { Menu("Add", systemImage: "plus") { Button(action: addTextLayer) { Text("Add Text Layer") } Button(action: addShapeLayer) { Text("Add Shape Layer") } } } } } private func addTextLayer() { if let design = designs.first { let newLayer = MYLayer(order: layers.count, kind: .text) newLayer.design = design modelContext.insert(newLayer) try? modelContext.save() } } private func addShapeLayer() { if let design = designs.first { let newLayer = MYShapeLayer(shapeName: "Ellipse", order: layers.count) newLayer.design = design modelContext.insert(newLayer) try? modelContext.save() } } } #Preview { ContentView() .modelContainer(for: [MYDesign.self, MYLayer.self, MYShapeLayer.self], inMemory: true) } @Model final class MYDesign { var title: String = "" @Relationship(deleteRule: .cascade, inverse: \MYLayer.design) var layers: [MYLayer] = [] init(title: String = "") { self.title = title } } @available(iOS 26.0, macOS 26.0, *) @Model class MYLayer { var design: MYDesign! var order: Int = 0 var title: String = "" init(order: Int = 0, title: String = "New Layer") { self.order = order self.title = title } } @available(iOS 26.0, macOS 26.0, *) @Model class MYShapeLayer: MYLayer { var shapeName: String = "" init(shapeName: String, order: Int = 0) { self.shapeName = shapeName super.init(order: order) } }
1
0
93
4d
SwiftUI Bug: Asset Catalog image for Regular Width Class not loaded
I've discovered what I think is a bug in SwiftUI unless I'm doing something obviously wrong. I have an Asset Catalogue configured with an image that has variants for "Any" width size class and "Regular" width size class. When the Image is loaded with the following code: struct ContentView: View { var body: some View { VStack { Image("icon") } .padding() } } when running on an iPad you would expect the image for the "Regular" width size class to be displayed. However, it seems that it is only ever the image for the "Any" width size class that is loaded. Is this a known issue with Asset Catalogs and SwiftUI?
1
0
94
5d
How to update TabViewBottomAccessoryPlacement
Playing around with the new TabViewBottomAccessoryPlacement API, but can't figure out how to update the value returned by @Environment(\.tabViewBottomAccessoryPlacement) var placement. I want to change this value programmatically, want it to be set to nil or .none on app start until user performs a specific action. (taps play on an item which creates an AVPlayer instance). Documentation I could find: https://developer.apple.com/documentation/SwiftUI/TabViewBottomAccessoryPlacement
4
3
269
5d
preferredImageDynamicRange is not marked as being available only for iOS 17 and above.
open var isAnimating: Bool { get } /// The preferred treatment to use for HDR images. By default the image view will defer to the value from its traitCollection. open var preferredImageDynamicRange: UIImage.DynamicRange /// The resolved treatment to use for HDR images. open var imageDynamicRange: UIImage.DynamicRange { get } This attribute is not marked as being applicable only in iOS 17+ versions. When viewing the UIImageView code in Xcode, the @available(iOS 17.0, *) annotation was not added, which resulted in successful compilation but caused a crash on iOS 16 devices.
3
0
73
5d
How to detect modifier keys with hardware keyboard in SwiftUI (iOS)?
Hi everyone, In UIKit, I can detect which key and modifier keys are pressed from an external hardware keyboard using the pressesBegan method in a UIResponder: override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) { for press in presses { if let key = press.key { print("Key: \(key.charactersIgnoringModifiers ?? "")") print("Modifiers: \(key.modifierFlags)") } } } I am now working in SwiftUI (iOS), and I couldn’t find a direct equivalent for pressesBegan. What is the recommended way in SwiftUI to detect modifier keys + key presses from an external keyboard? Is there a built-in API, or should I always wrap a UIKit view/controller for this purpose? Thanks in advance!
0
0
20
5d
Liquid Glass / iOS 26 Tab bar positions incorrectly on iPad; repositions correctly when re-opening app
I have a TabView (no modifiers) as the top-level view in my app. Starting with iOS 26 it starts off partially "under" the Status Bar, and then repositions if I switch between apps. Starting Point After Switching To/From Another App In the simulator, pressing "Home" and then reopening the app will fix it. Anyone else seeing something similar? Is there a modifier I'm missing on TabView that might prevent this behaviour? Thanks!
4
1
273
5d
Core Data + CKSyncEngine with Swift 6 — concurrency, Sendable, and best practices validation
Hi everyone, I’ve been working on migrating my app (SwimTimes, which helps swimmers track their times) to use Core Data + CKSyncEngine with Swift 6. After many iterations, forum searches, and experimentation, I’ve created a focused sample project that demonstrates the architecture I’m using. The good news: 👉 I believe the crashes I was experiencing are now solved, and the sync behavior is working correctly. 👉 The demo project compiles and runs cleanly with Swift 6. However, before adopting this as the final architecture, I’d like to ask the community (and hopefully Apple engineers) to validate a few critical points, especially regarding Swift 6 concurrency and Core Data contexts. Architecture Overview Persistence layer: Persistence.swift sets up the Core Data stack with a main viewContext and a background context for CKSyncEngine. Repositories: All Core Data access is abstracted into repository classes (UsersRepository, SwimTimesRepository), with async/await methods. SyncEngine: Wraps CKSyncEngine, handles system fields, sync tokens, and bridging between Core Data entities and CloudKit records. ViewModels: Marked @MainActor, exposing @Published arrays for SwiftUI. They never touch Core Data directly, only via repositories. UI: Simple SwiftUI views bound to the ViewModels. Entities: UserEntity → represents swimmers. SwimTimeEntity → times linked to a user (1-to-many). Current Status The project works and syncs across devices. But there are two open concerns I’d like validated: Concurrency & Memory Safety Am I correctly separating viewContext (main/UI) vs. background context (used by CKSyncEngine)? Could there still be hidden risks of race conditions or memory crashes that I’m not catching? Swift 6 Sendable Compliance Currently, I still need @unchecked Sendable in the SyncEngine and repository layers. What is the recommended way to fully remove these workarounds and make the code safe under Swift 6’s stricter concurrency rules? Request Please review this sample project and confirm whether the concurrency model is correct. Suggest how I can remove the @unchecked Sendable annotations safely. Any additional code improvements or best practices would also be very welcome — the intention is to share this as a community resource. I believe once finalized, this could serve as a good reference demo for Core Data + CKSyncEngine + Swift 6, helping others migrate safely. Environment iOS 18.5 Xcode 16.4 macOS 15.6 Swift 6 Sample Project Here is the full sample project on GitHub: 👉 [https://github.com/jarnaez728/coredata-cksyncengine-swift6] Thanks a lot for your time and for any insights! Best regards, Javier Arnáez de Pedro
3
0
382
5d
macOS 26 toolbar has wrong tint color sometimes in Dark Appearance
I have a SwiftUI Mac Catalyst app. I create a toolbar like this NavigationSplitView(columnVisibility: $sceneModel.columnVisibility, preferredCompactColumn: $preferredColumn) { sidebarView() } detail: { contentView() .toolbar { ToolbarItemGroup(placement: .topBarTrailing) { HStack { Button { sceneModel.onMaps(sender: self) } label: { Image(systemName: "map") .font(.title2) } Button { sceneModel.onSearch(sender: self) } label: { Image(systemName: "magnifyingglass") .font(.title2) } ... } } } } When my Mac Appearance is set to dark mode and the content under the toolbar is dark the toolbar looks good like this. But then if I have light content under the toolbar, the glass effect changes to light, but the tint on the icons stays white instead of changing to black and it is hard to see the icon. It looks like this. When I set the Appearance on my Mac to light, then the toolbar works just fine on both dark and light colored backgrounds. Does anyone know how I can fix this when the appearance is Dark?
8
0
139
6d
NSHostingSceneRepresentation doesn't show Window(_:id:) and UtilityWindow(_:id:) with .openWIndow(id:)
NSHostingSceneRepresentation, introduced in macOS 26, allows calling SwiftUI’s windows and other elements set in a Scene from AppKit. However, while Settings and WindowGroup set in the Scene can be invoked as expected using environment.openSettings() and environment.openWindow(id:) respectively, calling Window or WindowUtility doesn’t work. That is, the app just fails to open the desired window with the provided ID, and no error message or other feedback/crash/freeze appears. I expect that executing the openUtilityWindow(_:)action in the following code will display the UtilityWindow set in the scene. However, the window does not actually open. @main final class AppDelegate: NSObject, NSApplicationDelegate { private let scene = NSHostingSceneRepresentation { UtilityWindow("Utility Window", id: "UtilityWindow") { Text("Utility Window") .scenePadding() } } func applicationWillFinishLaunching(_ notification: Notification) { NSApp.addSceneRepresentation(self.scene) } @IBAction func openUtilityWindow(_ sender: Any?) { self.scene.environment.openWindow(id: "UtilityWindow") } } Is there something wrong with my implementation and expectation? Or is this a bug in NSHostingSceneRepresentation? Just in case, I’ve already filed this issue withFeedback Assistant: FB20310722 This feedback also includes a sample project reproducing this issue.
0
0
37
6d
Avoid using a segmented control in a toolbar
Hi, in the Human Interface Guidelines, Apple writes: Avoid using a segmented control in a toolbar. Toolbar items act on the current screen — they don’t let people switch contexts like segmented controls do. Along with this image: Source I'm confused by this example. The screenshot seems to be showing a segmented control in a toolbar. Is this saying that the Phone app's All/Missed toggle is different from a segmented control? Under iOS 26 it seems to take a different style compared to a regular segmented control. If so, which component is used to create this filter? Could you please clarify the guidelines? Thank you.
1
0
325
6d
How To Position Controls With SwiftUI
I am coming from C#, where Forms and Controls are placed similar to Swift Storyboards. I have been trying to learn Storyboards, but keep running across tutorials regarding SwiftUI, and Storyboard examples are few. So the question becomes, "how do I position controls on a Form using SwiftUI?" See the example below. I have run across many videos that use either horizontal or vertical positioning of controls, but these examples are usually very simple, with items occupying only the center portion of the screen. I get stuck on examples that are more complicated. The example below only shows the controls for the upper part of a Form, with some type of textbox (Viewform) below making up the rest of the Form. How does one make more complicated placement of controls with SwiftUI?
10
0
340
6d
Header Blur Effect on visionOS SwiftUI
Hi, I'm looking to build something similar to the header blur in the App Store and Apple TV app settings. Does anyone know the best way to achieve this so that when there is nothing behind the header it looks the same as the rest of the view background but when content goes underneath it has a blur effect. I've seen .scrollEdgeEffect on IOS26 is there something similar for visionOS? Thanks!
0
0
93
6d
MenuBarExtra with .window style: .onHover modifier doesn't work on macOS 26 Tahoe
List { Text("ITEM 1") .onHover(perform: { hovering in debugPrint("hovering: ", hovering) }) .help("ITEM 1") Text("ITEM 2") .onHover(perform: { hovering in debugPrint("hovering: ", hovering) }) .help("ITEM 2") Text("ITEM 3") .onHover(perform: { hovering in debugPrint("hovering: ", hovering) }) .help("ITEM 3") } .fixedSize(horizontal: false, vertical: true) .frame(maxHeight: 200) } Hello everyone!!! Considering the snippet above, seems like the onHover action, including help modifiers, doesn't work for the elements of a List, on macOS Tahoe. The situation changes using a ScrollView embedding a LazyVStack, or disabling Liquid Glass from the info plist, so my guess is that the new Liquid Glass style has something to do with this issue though I didn't find any clue about it. Does anyone have any idea? Maybe there's a layer above that doesn't allow to trigger the onHover modifier? Thanks in advance for your help!
0
0
105
1w
iOS 26 @FocusState Doesn't Work If TextField Is In Toolbar
When I add a TextField with @FocusState to a toolbar, I noticed that setting focus = false doesn't cause the form to lose focus If I move the TextField out of the toolbar setting focus = false works fine. How can I unfocus the text field when the cancel button is tapped? Minimal example tested on Xcode Version 26.0 beta 6 (17A5305f): import SwiftUI struct ContentView: View { @State private var text: String = "" @FocusState private var focus: Bool var body: some View { NavigationStack { List { Text("Test List") } .toolbar { ToolbarItem(placement: .bottomBar) { TextField("Test", text: $text) .padding(.horizontal) .focused($focus) } ToolbarItem(placement: .bottomBar) { Button(role: .cancel) { focus = false // THIS DOESN'T WORK! } } } } } } #Preview { ContentView() }```
1
2
230
1w