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

SwiftUI Documentation

Posts under SwiftUI subtopic

Post

Replies

Boosts

Views

Activity

Should ModelActor be used to populate a view?
I'm working with SwiftData and SwiftUI and it's not clear to me if it is good practice to have a @ModelActor directly populate a SwiftUI view. For example when having to combine manual lab results and clinial results from HealthKit. The Clinical lab results are an async operation: @ModelActor actor LabResultsManager { func fetchLabResultsWithHealthKit() async throws -> [LabResultDto] { let manualEntries = try modelContext.fetch(FetchDescriptor<LabResult>()) let clinicalLabs = (try? await HealthKitService.getLabResults()) ?? [] return (manualEntries + clinicalLabs).sorted { $0.date > $1.date }.map { return LabResultDto(from: $0) } } } struct ContentView: View { @State private var labResults: [LabResultDto] = [] var body: some View { List(labResults, id: \.id) { result in VStack(alignment: .leading) { Text(result.testName) Text(result.date, style: .date) } } .task { do { let labManager = LabResultsManager() labResults = try await labManager.fetchLabResultsWithHealthKit() } catch { // Handle error } } } } EDIT: I have a few views that would want to use these labResults so I need an implementation that can be reused. Having to fetch and combine in each view will not be good practice. Can I pass a modelContext to a viewModel?
4
0
151
3w
Display segmented control in iOS 26 with the previous rounded rectangle style
The default style for a segmented picker in iOS 26 is now a capsule shape. Is it possible to get back the previous rounded rectangle shape? In SwiftUI I have tried using the clipShape, containerShape, and background modifiers, applying a RoundedRectangle shape with a corner radius, to no avail. In UIKit I have tried to adjust the corner configuration for the UISegmentedControl, also to no avail. Am I missing something really obvious or is it impossible to change the style here? 🤔
2
0
150
3w
iPadOS 26 Disable "open recent"
How can I remove the "recents" section from long-pressing on my app icon? I've added the following to my AppDelegate, which removes it from the top MenuBar, but not from the app icon context menu. My app has registered a custom filetype, but it is not a document based app. Opening files imports them into the app's user library, and so does not make sense to have a "recents" list. override func buildMenu(with builder: any UIMenuBuilder) { super.buildMenu(with: builder) builder.remove(menu: .openRecent) }
1
0
158
3w
iOS 26 Home Screen Widgets Color Rendering Issue
Hi everyone! I've noticed a color rendering issue with Home Screen widgets on iOS 26: the colors displayed in widgets are inconsistent with those shown inside the app. At first, I suspected this might be caused by differences in color spaces, but even after explicitly specifying the color space for SwiftUI.Color or UIColor, the widget colors remain incorrect. Steps to reproduce: Create a new iOS project in Xcode 26 beta 6. Add a new Widget Extension target. Use the following Widget view code: struct MyWidgets: Widget { let kind: String = "MyWidgets" var body: some WidgetConfiguration { StaticConfiguration(kind: kind, provider: Provider()) { entry in let white = Color(.sRGB, red: 1, green: 1, blue: 1) let veryLightGray = Color(.sRGB, red: 0.96, green: 0.96, blue: 0.96) let lightGray = Color(.sRGB, red: 0.9, green: 0.9, blue: 0.9) VStack(spacing: 0) { Rectangle() .foregroundStyle(veryLightGray) // 👈 Rectangle() .foregroundStyle(lightGray) // 👈 } .containerBackground(white, for: .widget) // 👈 } .configurationDisplayName("My Widget") } } ⬆️ In-app, the colors are correct: the top block is a very light gray (white=0.96)✅, and the bottom block is a regular gray (white=0.90)✅. However, on the Home Screen widget, the result is as follows: ⬆️ The top light gray block blends completely into the white background and is indistinguishable; the bottom gray block also appears lighter than it does in-app ❌. This issue occurs both on the simulator and on real devices. (Interestingly, the colors are correct in the Xcode Preview.) Whether I declare colors in code (SwiftUI.Color or UIColor) or in the Asset Catalog, the widget's color rendering does not match expectations. What's even stranger is that if I add an extra pure white block (white=1.0) to the view, it immediately affects all the colors in the widget: This whole behavior makes it very difficult to set accurate colors for widgets on iOS 26. While it seems related to glass effect rendering and color space handling, I still feel there might be a bug in the implementation.
1
0
88
3w
How to Start with Default Apple Maps Functionality in iOS 18+
Hi everyone, I’m building an iOS 18+ app in Xcode where Apple Maps is central. My goal is to start with the default Apple Maps experience (map view, search bar, pins, directions sheet, etc.) and then customize it for my project. I tried coding it from scratch using MapKit and SwiftUI, but I wasn’t able to get full parity with the basic features of the Maps app. My questions: Is there any sample project, template, or reference that provides the default Apple Maps functionality (views + interactions) as a baseline? Can I copy these into my Xcode project and then extend/customize them? If not, what’s the recommended best practice to get as close as possible to the native Maps app before adding my own features? Any guidance, sample code, or documentation links would be greatly appreciated.
1
0
92
3w
iOS 26, SwiftUI .sheet Background Color has Gray/Green Tint on iPad
On iPadOS 26 (up to beta 7), .sheet backgrounds have a dark green tint on Dark Mode and a gray tint on Light Mode. This is clearly noticeable on both the Canvas/Simulator and a physical device. Here's a sample View that shows the issue: import SwiftUI struct ContentView: View { @State private var isPresenting: Bool = false var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") Button("Show Sheet") { isPresenting.toggle() } } .sheet(isPresented: $isPresenting) { VStack { HStack { Spacer() Button("Cancel", systemImage: "xmark.circle.fill") { } .foregroundStyle(.secondary) .labelStyle(.iconOnly) .buttonStyle(.plain) .contentShape(.circle) } TabView { Tab("Tab 1", systemImage: "cart") { Text("Hello, tab 1") } Tab("Tab 2", systemImage: "cart") { Text("Hello, tab 2") } } } .scenePadding() } .padding() .preferredColorScheme(.dark) } } #Preview { ContentView() } Is this the expected behavior with the new OS? Anyone else seeing this?
Topic: UI Frameworks SubTopic: SwiftUI
1
0
157
3w
How can I connect NSTableCellView.textField to a SwiftUI view?
When using NSTableView or NSOutlineView, if you use an NSTableCellView and wire up the .imageView and .textField properties then you get some "free" behaviour with respect to styling and sizing of those fields. (ex: They reflect the user's preferred "Sidebar Icon Size" as selected in Settings. ) If I'm using a SwiftUI View inside an NSTableCellView, is there any way to connect a Text or Image to those properties? Consider the following pseudo code: struct MyCellView: View { let text: String let url: URL? var body: some View { HStack { Image(...) // How to indicate this is .imageView? Text(...) // How to indicate this is .textField? } } } final class MyTableCellView: NSTableCellView { private var hostingView: NSHostingView<MyCellView>! init() { self.hostingView = NSHostingView(rootView: MyCellView(text: "", url: nil)) self.addSubview(self.hostingView) } func configureWith(text: String, url: URL) { let rootView = MyCellView(text: text, url: url) hostingView.rootView = rootView // How can I make this connection? self.textField = rootView.??? self.imageView = rootView.??? } } I'm ideally looking for a solution that works on macOS 15+.
2
0
85
3w
CarPlay CPGridTemplate – buttons show in a row, and limited to 9 buttons
Hi, I’m working with CPGridTemplate in CarPlay. According to the documentation, it supports up to 9 CPGridButton objects and should display them in a grid (up to 3×3). However, I’ve run into two issues: Row instead of grid When I add 4–6 buttons, they don’t appear in a 2×2 or 2×3 grid. Instead, they are shown in a single horizontal row. Even 9 buttons do not appear in a 3x3 grid. More than 9 buttons My use case requires more than 9 icons, but it looks like CPGridTemplate ignores any additional buttons beyond the first 9. Is there any supported way to display more than 9 buttons in a grid, or is pagination/multiple templates the only option? Thanks in advance!
1
0
57
3w
Keyboard Toolbar Padding iOS26
When I create a SwiftUI toolbar item with placement of .keyboard on iOS 26, the item appears directly on top of and in contact with the keyboard. This does not look good visually nor does it match the behavior seen in Apple's apps, such as Reminders. Adding padding to the contents of the toolbar item only expands the size of the item but does not separate the capsule background of the item from the keyboard. How can I add vertical padding or spacing to separate the toolbar item capsule from the keyboard?
Topic: UI Frameworks SubTopic: SwiftUI
3
3
249
3w
DocumentBrowser toolbar behavior in SwiftUI apps
I’m building a document-based SwiftData app (iPhone/iPad/Mac). Here’s a minimal example of how I’m using DocumentGroup. DocumentGroup(editing: Trip.self, contentType: .trips) { ContentView() } if #available(iOS 18.0, *) { DocumentGroupLaunchScene { NewDocumentButton("New Trip") } } I’m struggling with the toolbar behavior in DocumentGroup apps. My content view uses a TabView, and each tab contains a NavigationSplitView. After I select a document in the document browser, I see my tabs. Regardless of which tab is selected, there’s a navigation bar showing the document name and a back button to the document browser. However, only the first tab shows the disclosure button to rename the document. I’d expect to be able to rename the document anywhere the name is shown. When I navigate to the detail view of my NavigationSplitView (or when using NavigationView/NavigationStack), I still see that back button to the document browser. When the user taps it, they expect to go back to the previous view, not to the document browser. What’s really odd is that even sheet or fullScreenCover presentations include these document UI elements in the navigation bar. I can’t get rid of them. Even if I set a title via the toolbar or navigationTitle, the rename disclosure button remains visible. Do DocumentGroup apps intentionally show their specific navigation bar everywhere? Is this a bug or expected behavior? And is it expected that the rename disclosure button appears only on the first tab of a TabView?
4
0
160
3w
Sharelink dismisses Parent View
Hello Folks, what's causing the "smart" dismiss after shareLink? this only happens when saving the photos. view -> opens a popover -> sharelink -> save to photo -> Allow photo Access -> ✅ view -> opens a popover -> sharelink -> save to photo -> [Observe popover dismisses ❌] popover should stay open Remember to add INFOPLIST_KEY_NSPhotoLibraryAddUsageDescription = save to album in project settings import SwiftUI @main struct sharepopoverDismissApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @State private var showingSharePopover = false @State private var urlToShare: URL = Bundle.main.url(forResource: "BigBuckBunny", withExtension: "mp4") ?? URL(string: "https://www.example.com")! var body: some View { VStack { Button { showingSharePopover = true } label: { Label("Show Share Options", systemImage: "square.and.arrow.up") .font(.title2) .padding(.vertical, 10) .padding(.horizontal, 20) } .buttonStyle(.borderedProminent) .tint(.indigo) .controlSize(.large) .shadow(radius: 5) .popover(isPresented: $showingSharePopover, attachmentAnchor: .point(.center)) { ShareLinkPopoverView(url: urlToShare) } } .padding() } } struct ShareLinkPopoverView: View { let url: URL var body: some View { VStack(spacing: 20) { ShareLink(item: url) { Label("Share Now", systemImage: "square.and.arrow.up") .font(.headline) .padding(.vertical, 8) .padding(.horizontal, 15) } .interactiveDismissDisabled() .buttonStyle(.borderedProminent) .tint(.green) .controlSize(.regular) } .padding(20) .presentationCompactAdaptation(.popover) } }
3
0
120
3w
Toolbar button is clipped in iOS 26
When using a custom button style to generate the button label, it will be clipped. You can check below sample, the first one is render fine, but the second button with custom style will be clipped. struct ContentView: View { var body: some View { NavigationStack { List {} .toolbar { ToolbarItem(placement: .topBarLeading) { Button { } label: { HStack { Image(systemName: "chevron.backward") Text("Back") } } } ToolbarItem(placement: .topBarLeading) { Button { } label: { EmptyView() } .buttonStyle(MyButtonStyle()) } } .navigationTitle("Title") } } } struct MyButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { HStack { Image(systemName: "chevron.backward") Text("Back") } } }
Topic: UI Frameworks SubTopic: SwiftUI
4
0
179
3w
iOS 26 Beta 5 SwiftUI Picker(.pickerStyle(.segmented)) + Images(.symbolRenderingMode(.palette)) + Sheet = Freeze
App freezes when using a SwiftUI Picker with pickerStyle(.segment), Images with .symbolRenderingMode(.palette) and try to show a sheet. import SwiftUI enum Sample: String, CaseIterable, Identifiable { case aaa, bbb, ccc, ddd var id: Self { self } var image: Image { switch self { case .aaa: Image(systemName: "square.stack.3d.up.fill") case .bbb: Image(systemName: "square.and.arrow.up") case .ccc: Image(systemName: "square.and.arrow.up.fill") case .ddd: Image(systemName: "square.and.arrow.down") } } } struct PickerBug: View { @State private var selected: Sample = .aaa @State var showSheet = false var body: some View { List { Section { VStack { Picker("Qqq", selection: $selected) { ForEach(Sample.allCases) { sample in sample.image // .symbolRenderingMode(.palette) .foregroundStyle(.black) } } .pickerStyle(.segmented) } } Text("Aaa") } .toolbar { ToolbarItem(placement: .topBarTrailing) { Button { showSheet.toggle() } label: { Text("A") } } } .sheet(isPresented: $showSheet) { Text("Aaaa") } } } #Preview { NavigationStack { PickerBug() } } Uncomment .symbolRenderingMode(.palette) show the picker and the app freezes. Xcode, Preview and Simulator, i could not test on a real device.
0
0
97
3w
What does Observable protocol (not macro) conformance get you?
Hi, In the Apple Scrumdinger sample, the SpeechRecognizer class conforms to the Observable protocol: public actor SpeechRecognizer: Observable { public enum RecognizerError: Error { case nilRecognizer . . . The developer help text suggests that the protocol conformance does not add observation functionality. This class does not use the @Observable macro. So, how does this work under the hood?
1
0
73
3w
Ignore Home Screen Tint/Accents in Colored Components
I'm having some trouble getting my widget to display how I want when the user has a tint applied to their home screen. The issue I'm having is with a Text() element, as well as a LinearGradient I am displaying on top of my image. The text should always be white, and the gradient is always black with varying levels of opacity. I've managed to fix this issue with images displayed in my widget by leveraging widgetAccentedRenderingMode(.fullColor) however, there does not seem to be an equivalent of this for non-Image components. I'm aware of .widgetAccentable(false) but as I understand it, elements are already considered not accentable by default and you need to explicitly declare widgetAccentable(true) to add them to the accent group. I've tried specifying this to be false up and down my view hierarchy just to see if something will stick but no luck. Are there any other levers I can pull to preserve the declared colors for my text and gradient components? The images I am displaying is album artwork where preserving the original image is integral, but the tinted text color and overlaid gradient often clash or just looks bad in general. Is there a solution for colored primitive elements?
0
0
55
3w
Concentric corners not working
I want the gray view to have concentric corners with the device border. That works. Then I want the blue rectangle to have concentric corners with the gray view. That does not work. Instead the blue rectangle is also concentric with the device border. Once I add other content like a Text element, the corner radius breaks. How can I make this work? .containerShape does not take a ConcentricContainerShape. struct ContentView: View { var body: some View { List { Text("Content") } .overlay(alignment: .bottom) { content } .ignoresSafeArea(.all, edges: .bottom) } var content: some View { VStack(alignment: .leading) { Rectangle() .foregroundStyle(.blue) .frame(width: 100, height: 100) .clipShape(.rect(corners: .concentric, isUniform: true)) Text("Custom Container") } .padding(20) .frame(maxWidth: .infinity, alignment: .leading) .background(Color.gray, in: .rect(corners: .concentric, isUniform: true)) .padding(15) } }
2
0
79
3w
How to create an overlay with padding that ignores the safe area?
Was it always so tricky to ignore the bottom safe area? Seems like iOS 26 makes this much harder. I've tried many variations of ignoresSafeArea, safeAreaInset, safeAreaBar, etc. Nothing seems to work. As soon as I add padding, the bottom safe area crashes the party. This is what I want to achieve: This is what I get right now: struct ContentView: View { var body: some View { List { Text("Content") } .overlay(alignment: .bottom) { content } } var content: some View { VStack { Text("Custom Container") } .frame(maxWidth: .infinity) .frame(height: 400) .background(Color.gray, in: .rect(corners: .concentric, isUniform: true)) .padding(15) } }
3
0
82
3w
How do I use containerRelative on a grid in my widget?
I want to display a grid of items in my widget similar to the systemLarge Shortcuts app widget. I use clipShape(.containerRelative) to get the widget corner radius, but items that do not touch a corner in any way do not get this treatment. This is even worse with the extra large widget. How can I apply the corner radius of the widget minus the padding across all items? It does not seem like the radius is exposed outside of that special shape.
1
0
64
4w