When I migrate to swift6.2, the photopicker always giving the warning like below:
Call to main actor-isolated initializer 'xxxfunction' in a synchronous nonisolated context
it's so weird,because no matter it's a viewbuilder or a struct view,it can't fix the warning.
SwiftUI
RSS for tagProvide views, controls, and layout structures for declaring your app's user interface using SwiftUI.
Posts under SwiftUI tag
200 Posts
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi all - i'm encountering a strange issue since updating to Xcode 26.0.1.
It seems that any SwiftUI Views that have an :ObservedObject property that contains @Published properties, and use those properties inside the View, no longer update when those properties are updated when the view also has an @Environment property.
If I remove the @Environment property and any usage of it, the view updates correctly.
The specific environment property i'm using is .safeAreaInsets, like so:
@Environment(\.safeAreaInsets) private var safeAreaInsets
Is this a recognised bug in the latest iOS 26 SDK?
Thanks
I'm currently using the extension below to determine whether an iPhone has a notch so I can adjust my UI accordingly.
extension UIDevice {
var hasNotch: Bool {
if userInterfaceIdiom == .phone, let window = (UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow }) {
return window.safeAreaInsets.bottom > 0
}
return false
}
}
(Adapted from https://stackoverflow.com/questions/73946911/how-to-detect-users-device-has-dynamic-island-in-uikit)
This no longer works in iOS 26, and I have yet to find a similar method that works. Does anyone have any fixes?
Hi,
I am looking for comprehensive "controls" documentation for SwiftUI.
When searching on the Apple site, I have not seen any documentation that shows a picture of a control and an explantation of what it does, for all the available controls.
I have also searched the web for such documentation, and have not found any.
When I was learning Rust, I came across a beautiful document about GTK3 (PDF) that had a complete list of controls, including pictures. If something looked interesting I could do a search for more details.
Thanks, Jim
I noticed that with iOS 18, when adding a widget to the Control Center, there is now some "grouping system". I'm interested in the Capture group, which contains native widgets from Apple as well as third party apps like Instagram and Blackmagic cam, widgets in this group open the camera. My widget also opens the camera in my app, how can I add it to this group?
Topic:
App & System Services
SubTopic:
Widgets & Live Activities
Tags:
SwiftUI
WidgetKit
App Intents
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.
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() }
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).
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.
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)
}
}
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?
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
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.
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!
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!
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
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?
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.
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?
The Widget Image display on iOS26 transparency mode is blank
Both of these writing methods show blank Spaces. Is there a problem with my code or a bug in the system?
The current compilation environment is Xcode Version 16.3 (16E140) iOS26.0