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

Posts under SwiftUI tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

SwiftUI inspector inside NavigationStack causes scrolling and transition issues
We’ve encountered an issue while developing with SwiftUI: when using the inspector on iPadOS, if the inspector is placed inside a NavigationStack, and both the view attached to the inspector and the content inside the inspector itself are scrollable, scrolling them to the top may cause abnormal jitter. We suspect this issue might be related to NavigationTitle. However, if we place the inspector outside the NavigationStack, tapping any NavigationLink while the inspector is expanded will cause problems with the View.matchedTransitionSource(id:in:) animation. A reproducible project can be found here: https://github.com/ThreeManager785/Inspetor-Issue We’ve tried many approaches but haven’t been able to resolve it. Is there any way to fix this issue?
0
0
59
7h
Mutating an array of model objects that is a child of a model object
Hi all, In my SwiftUI / SwiftData / Cloudkit app which is a series of lists, I have a model object called Project which contains an array of model objects called subprojects: final class Project1 { var name: String = "" @Relationship(deleteRule: .cascade, inverse: \Subproject.project) var subprojects : [Subproject]? init(name: String) { self.name = name self.subprojects = [] } } The user will select a project from a list, which will generate a list of subprojects in another list, and if they select a subproject, it will generate a list categories and if the user selects a category it will generate another list of child objects owned by category and on and on. This is the pattern in my app, I'm constantly passing arrays of model objects that are the children of other model objects throughout the program, and I need the user to be able to add and remove things from them. My initial approach was to pass these arrays as bindings so that I'd be able to mutate them. This worked for the most part but there were two problems: it was a lot of custom binding code and when I had to unwrap these bindings using init?(_ base: Binding<Value?>), my program would crash if one of these arrays became nil (it's some weird quirk of that init that I don't understand at al). As I'm still learning the framework, I had not realized that the @model macro had automatically made my model objects observable, so I decided to remove the bindings and simply pass the arrays by reference, and while it seems these references will carry the most up to date version of the array, you cannot mutate them unless you have access to the parent and mutate it like such: project.subcategories?.removeAll { $0 == subcategory } project.subcategories?.append(subcategory) This is weirding me out because you can't unwrap subcategories before you try to mutate the array, it has to be done like above. In my code, I like to unwrap all optionals at the moment that I need the values stored in them and if not, I like to post an error to the user. Isn't that the point of optionals? So I don't understand why it's like this and ultimately am wondering if I'm using the correct design pattern for what I'm trying to accomplish or if I'm missing something? Any input would be much appreciated! Also, I do have a small MRE project if the explanation above wasn't clear enough, but I was unable to paste in here (too long), attach the zip or paste a link to Google Drive. Open to sharing it if anyone can tell me the best way to do so. Thanks!
0
0
53
12h
macOS Document-Based SwiftUI App: “Save” menu item not localized in French
Hi everyone, I’ve run into a strange localization issue with macOS document-based apps in SwiftUI/AppKit. I created a standard document-based macOS app in Xcode (SwiftUI template) and added a French localization to the project. All system-generated menu bar commands (File → New, Close, Print, etc.) are correctly translated into French… except for “Save”, which remains in English. To rule out problems in my own code, I created a fresh, unmodified document-based app project in Xcode, and immediately added French localization without touching any code. Same result: all commands are translated except “Save”. This suggests the issue isn’t specific to my app code, but either the project template, or possibly macOS itself. My environment • Xcode version: 16.4 • macOS version: 15.6.1 Sequoia] • Swift: Swift 6 Questions 1. Has anyone else seen this issue with the “Save” command not being localized? 2. Is this expected behavior (maybe “Save” is handled differently from other menu items)? 3. If it’s a bug in the template or OS, is there a known workaround? Thanks for any insights P.S. Please note that I'm a total beginner
2
0
61
3h
WebPage doesn't seem to update view when changed in SwiftUi
I tried the new WebView api in swiftui and tried to pass webPage for this view to be able to control the navigation of the user by giving him the option to go back or forward using nav buttons but the view doesn't get's updated when the webPage.backForwardList.backList so the buttons remains disabled. code snippet: @available(iOS 26, *) struct LinkWebViewFor26: View { let url: URL @State var webPage = WebPage() @Environment(\.dismiss) private var dismiss var body: some View { WebView(webPage) .webViewBackForwardNavigationGestures(.disabled) .task { webPage.load(url) } .toolbar { ToolbarItem(placement: .topBarTrailing) { Button { dismiss() } label: { Image(systemName: "checkmark") } .buttonStyle(.glassProminent) } ToolbarItemGroup(placement: .topBarLeading) { BackForwardMenu( list: webPage.backForwardList.backList, label: .init(text: "Backward", systemImage: "chevron.backward") ) { item in webPage.load(item) } BackForwardMenu( list: webPage.backForwardList.forwardList.reversed(), label: .init(text: "Forward", systemImage: "chevron.forward") ) { item in webPage.load(item) } } } .onChange(of: webPage.backForwardList) { _, _ in print(webPage.backForwardList.backList) } } }
1
0
66
12h
How to add blur around the Tab bar in iOS 26
I have a TabView and in one of the tabs I have a horizontal ScrollView that uses .scrollTargetBehavior(.paging) in each of those pages I have a List, but the bottom of the list around the Tab bar doesnt get blurred, if I remove the horizontal scrollview and just have a list it blurs fine. So I want to know how I can manually add the blur when using the horizontal scrollview
0
0
76
1d
How to disable scrollToTop when clicking on selected tab
I currently have a SwiftUI TabView that has 5 Tab's. The first tab has a UIScrollView in a UIViewRepresentible with scrollView.scrollsToTop = false and that works fine for when the user hits the navigation bar, however if the user taps the first tab when it is already selected my UIScrollView scrolls to top. My UIScrollView is essentially 5 views, a center view, top, bottom, right, and left view. All views except for the center are offscreen but available for the user to scroll horizontal or vertical (and the respective views get updated based on the new center view). The issue I have is that clicking the first tab when its already selected, sets the content offset (for the y axis) to 0, which messes me up 2x, first it scrolls up but since its not really scrolling the right, left, and upper views dont exist, which makes the user think it can't be scrolled or it's broken. For now I subclassed UIScrollView like this class NoScrollToTopScrollView: UIScrollView { override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) { if contentOffset.y == .zero { // Ignore SwiftUI’s re-tap scroll-to-top return } super.setContentOffset(contentOffset, animated: animated) } } which seems to work, but I'm just wondering if there is a better way to do this, or maybe a way to disable SwiftUI Tab from doing its default action which can help with a SwiftUI ScrollView as well?
0
0
69
1d
SwiftUI Nav Bar Changes in Height When Loading While Presented in a Sheet
If you create a SwiftUI App where a ‘.sheet’ is presented and use a NavigationStack within that Sheet, when you use NavigationLink to present a view, the title of the Nav Bar will start at a height of 46px and pop to the Default Height of 54px when it loads causing a visual pop in the UI. In iOS 18 it functions correctly, in iOS 26 the visual pop is present. This impacts both inline and large styles, if you disable the back button it is still present, the only way I have discovered to get rid of it is by using 'fullScreenCover' instead of '.sheet'. This feels like buggy UI. This issue has been present since iOS 26 Beta 5, I was hoping it would be fixed but is still present in the GM. Feedback has been filed via Feedback Assistant: FB20228369 This is the code to re-produce the issue: import SwiftUI struct ContentView: View { @State private var showSheet: Bool = false var body: some View { VStack { Button { showSheet.toggle() } label: { Text("Show Sheet") } } .padding() .sheet(isPresented: $showSheet) { NavigationStack { List { NavigationLink { Rectangle() .foregroundStyle(.red) .navigationTitle("Red") } label: { Text("Show Red") } } } .presentationSizing(.page) } } } #Preview { ContentView() }
0
1
251
2d
Custom SF Symbols with badges not vertically centered in SwiftUI buttons
[Also submitted as FB20225387] When using a custom SF Symbol that combines a base symbol with a badge, the symbol doesn’t stay vertically centered when displayed in code. The vertical alignment shifts based on the Y offset of the badge. There are two problems with this: The base element shouldn’t move vertically when a badge is added—the badge is meant to add to the symbol, not change its alignment. The badge position should be consistent with system-provided badged symbols, where badges always appear in a predictable spot relative to the corner they're in (usually at X,Y offsets of 90% or 10%). Neither of these behaviors match what’s expected, leading to inconsistent and misaligned symbols in the UI. Screenshot of Problem The ellipsis shifts vertically whenever the badge Y offset is set to anything other than 50%. Even at a 90/10 offset, it still doesn’t align with the badge position of the system "envelope.badge" symbol. SF Symbols Export This seem to be a SwiftUI issue since both the export from SF Symbols is correctly centered: Xcode Assets Preview And it's also correct in the Xcode Assets preview: Steps to Repro In SF Symbols, create a custom symbol of "ellipsis" (right-click and Duplicate as Custom Symbol) Combine it with the "badge" component (select Custom Symbols, select the newly-created "custom.ellipsis", then right-click and Combine Symbol with Component…) Change the badge's Y Offset to 10%. Export the symbol and add it to your Xcode asset catalog. In Xcode, display the symbol inside a Button using Image(“custom.ellipsis.badge”). Add a couple more buttons separated by spacers, using system images of "ellipsis" and "app.badge". Compare the "custom.ellipsis.badge" button to the two system symbol buttons. Expected The combined symbol remains vertically centered, matching the alignment shown in both the SF Symbols export window and the Xcode asset catalog thumbnails. Actual The base symbol (e.g., the ellipsis portion) shifts vertically based on the Y offset of the badge element. This causes visual misalignment when displayed in SwiftUI toolbars or other layouts. Also included the system “envelope.badge” icon to show where a 90%, 10% badge offset should be located. System Info SF Symbols Version 7.0 (114) Xcode Version 26.0 (17A321) macOS 15.6.1 (24G90) iOS 26.0 (23A340)
0
0
183
2d
Strange Liquid Glass elements tinting
I updated my App to iOS26 and I have some strange tinting with Liquid Glass elements. Sometime the elements are tinted and sometime not. Look at the screenshots. This is the same view, without any changes. Just go back and forth to see the different Button tinting. And this is not just happing in the View. It happens all over the app. Why is this happening?
2
0
131
1d
SwfitUI withAnimation's completion not called on iOS 26
I'm adapting my app on iOS 26, and I just found out withAnimation fuction's completion not called in some cases. The same code on iOS 18 was fine. The problem is very fatal, When you check the api, it saids "The completion callback will always be fired exactly one time",but this time it doens't work. I'm using the Xcode Version 26.0 (17A321) RC1,still not test on a real devcie yet.
1
0
111
3d
SwiftUI Button with Image view label has smaller hit target
[Also submitted as FB20213961] SwiftUI Button with a label: closure containing only an Image view has a smaller tap target than buttons created with a Label or the convenience initializer. The hit area shrinks to the image bounds instead of preserving the standard minimum tappable size. SCREEN RECORDING On a physical device, the difference is obvious—it’s easy to miss the button. Sometimes it even shows the button-tapped bounce animation but doesn’t trigger the action. SYSTEM INFO Xcode Version 26.0 (17A321) macOS 15.6.1 (24G90) iOS 26.0 (23A340) SAMPLE CODE The following snippet shows the difference in hit targets between the convenience initializer, a Label, and an Image (the latter two in a label: closure). // ✅ Hit target is entire button Button("Button 1", systemImage: "1.square.fill") { print("Button 1 tapped") } // ✅ Hit target is entire button Button { print("Button 2 tapped") } label: { Label("Button 2", systemImage: "2.square.fill") } // ❌ Hit target is smaller than button Button { print("Button 3 tapped") } label: { Image(systemName: "3.square.fill") }
1
2
124
3d
.safeAreaBar(edge: .bottom), animation lag on appear/disappear
When I try to show/hide the content in .safeAreaBar(edge: .bottom), especially the content with a large height, the background animation of the toolbar is very laggy. iOS 26 RC Feedback ID - FB19768797 import SwiftUI struct ContentView: View { @State private var isShown: Bool = false var body: some View { NavigationStack { Button("Toggle") { withAnimation { isShown.toggle() } } ScrollView(.vertical) { ForEach(0..<100) { index in Text("\(index)") .padding() .border(.blue) .background(.blue) .frame(maxWidth: .infinity) } } .scrollEdgeEffectStyle(.soft, for: .bottom) .safeAreaBar(edge: .bottom) { if isShown { Text("Safe area bar") .padding(64) .background(.red) } } } } } #Preview { ContentView() }
0
0
59
3d
Popovers are broken on macCatalyst
.popover(isPresented: modifier doesn't work on Mac Catalyst when attached to the item in the toolbar. The app crashes on button click, when trying to present the popover. iOS 26 RC (macOS 26 RC) Feedback ID - FB20145491 import SwiftUI struct ContentView: View { @State private var isPresented: Bool = false var body: some View { NavigationStack { Text("Hello, world!") .toolbar { ToolbarItem(placement: .automatic) { Button(action: { self.isPresented.toggle() }) { Text("Toggle popover") } .popover(isPresented: $isPresented) { Text("Hello, world!") } } } } } } #Preview { ContentView() }
0
0
45
3d
.safeAreaBar doesn't look good together with .searchable
When using .scrollEdgeEffectStyle(.hard, for: .top), search bar background doesn't match with . safeAreaBar view background (you can see that is is darker than the rest of the toolbar). Please find the screenshot in the attachment iOS 26 RC Light Mode Feedback ID - FB19768159 import SwiftUI struct ContentView: View { @State private var count = 0 var body: some View { NavigationStack { ScrollView(.vertical) { ForEach(0..<100) { index in Text("\(index)") .background(.red) } } .scrollEdgeEffectStyle(.hard, for: .top) .searchable( text: .constant(""), placement: .navigationBarDrawer(displayMode: .always) ) .safeAreaBar(edge: .top) { Text("Safe area bar") } } } } #Preview { ContentView() }
0
0
31
3d
.preferredColorScheme doesn't work with .scrollEdgeEffectStyle(.hard, for: .top)
.preferredColorScheme(.dark) doesn't seem to affect app toolbar, when .scrollEdgeEffectStyle(.hard, for: .top) is used. In the attachment, you can see the title and toolbar items don't change according to current value of .preferredColorScheme (you may need to scroll a bit to achieve it) iOS 26 RC Feedback ID - FB19769073 import SwiftUI struct ContentView: View { var body: some View { NavigationStack { ScrollView(.vertical) { ForEach(0..<100) { index in Text("\(index)") .padding() .border(.blue) .background(.blue) .frame(maxWidth: .infinity) } } .scrollEdgeEffectStyle(.hard, for: .top) .navigationTitle("Test Title") .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .navigationBarTrailing) { Button(action: { }) { Text("Test") } } } } .preferredColorScheme(.dark) } } #Preview { ContentView() }
0
0
33
3d
Liquid Glass material behaviour question
I have two views I've applied Liquid Glass to in Swift UI. I've noticed that depending on the height of the view the material changes and I'm not sure why. See the attached screenshot. Both views add the liquidGlass style in the same way but behave very differently on the same background. Ideally I'd like them to look the same as the bottom one. Is that the same as the clear style?
1
1
214
4d
SwiftUI DocumentGroup blocks "Create Document" button when opening a document in conflict state
In a SwiftUI DocumentGroup, the "Create Document" button remains permanently disabled when attempting to open a document that is in a conflict state (e.g., due to simultaneous edits across devices). As a result, the user cannot create new documents, and the app becomes stuck. On macOS, the expected conflict resolution dialog appears, and the app continues to function normally. On iOS, however, the "Create Document" button stays disabled indefinitely. This behavior occurs consistently in a default SwiftUI document-based app. Steps to Reproduce: Create a new SwiftUI document-based project in Xcode; Setup iCloud Storage in Signing & Capabilities; Create an empty document and place it in a conflict state (e.g., save it simultaneously from two devices); Attempt to open the conflicted document on an iPhone or iPad; Expected Result: The user should be able to resolve the conflict and continue working; The "Create Document" button should remain functional; Actual Result: The "Create Document" button is disabled permanently; The app cannot create new documents until restarted; Environment; iOS 18, iOS 26 (latest tested); Xcode Version 16.4 (16F6) Reproduced on iPhone and iPad; Works as expected on macOS; This appears to be a blocking issue in SwiftUI’s DocumentGroup on iOS. FB20203775
0
0
62
4d
Country from MKReverseGeocoding
As GeoCoder is now deprecated I am struggling to get the country only information from the new MKReverseGeocoding. Maybe someone can guide me or give me direction? Or is this just not possible anymore? let request = MKReverseGeocodingRequest(location: self.lastLocation ?? fallbackLocation) request?.getMapItems { items, error in guard let items = items else { return } self.cityName = items.first?.addressRepresentations?.cityWithContext ?? "" self.countryName = items.first?.addressRepresentations?.regionName ?? "" } I couldn't find anything here, sure you can get the full Address but I need single values to store so the user can search for (example City, Country) In case the structure is always the same, let us say the country is always third part, sure I could split the string but it is not a reliable way to do this, at least for me. Any help would be much appreciated.
1
0
61
4d
Why does SwiftUI Text(date, style: .relative) show the same duration for different dates?
I’m using SwiftUI’s Text(_:style:) with the .relative style to show how long ago a date occurred. According to the docs: A style displaying a date as relative to now. I expected it to show the precise difference between a past date and the current date. However, I noticed that two dates that are 3 days apart both display the same relative string under certain conditions. Code snippet to reproduce- (using GMT time zone and the system calendar) IMPORTANT: To reproduce this, set your Mac’s system clock to 8 September 2025, 3:00 AM. SwiftUI’s relative style uses the current system time as its reference point, so changing the clock is necessary to see the behavior. Settings Mac is set to Central European Time zone (but this behaviour was also reproduced by one of my app's users in the US.) Mac OS Sequoia 15.5 XCode 16.4 tested on an iOS Simulator and a real iPhone both running iOS 18.5 struct TestDateView: View { var body: some View { // 8. July 10AM to 8. September 3AM = Shows 2 months 2 days let startDate1: Date = Calendar.current.date(from: .init(calendar: .current, timeZone: .gmt, year: 2025, month: 7, day: 8, hour: 10, minute: 0, second: 0))! // 5. July 10AM to 8. September 3AM = Shows 2 months 2 days let startDate2: Date = Calendar.current.date(from: .init(calendar: .current, timeZone: .gmt, year: 2025, month: 7, day: 5, hour: 10, minute: 0, second: 0))! // IMPORTANT!: Need to set MAC's clock to 8. September 3:00 AM to reproduce this bug VStack { Text(startDate1, style: .relative) Text(startDate2, style: .relative) } } } How exactly does the .relative style work internally? Is it expected that different dates can collapse into the same result like this, or is there a better way to use .relative to get more precise results? PS: I know about DateComponents and DateFormatter for exact calculations, but I’d like to understand this approach since it auto-updates natively with no timers or publishers.
0
0
68
4d
SwiftUI Table Header Background not appearing in iPadOS 26
Hi, it seems like using Table on iPadOS 26 results in the table header not applying a background. When comparing the same code on iPadOS 18, the table header applies a blur behind the header to ensure legibility when the user scrolls the content. Is there a way to ensure Table applies a background effect to the header so that content remains legible during scrolling? Here is a minimal example: struct TablePreviewContent: Identifiable { var id: Int { text.hashValue } var text: String } #Preview { let content = [TablePreviewContent(text: "Hello"), TablePreviewContent(text: "World")] Table(content) { TableColumn("Title", value: \.text) } } I've attached screenshots of the behavior on iPadOS 26 compared to iPadOS 18 to illustrate the issue.
2
0
78
4d