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

Preview crashes when using ForEach with a Binding to an array and generics
The following repro case results in a previews crash on Xcode 26 beta 3 (report attached). FB18762054 import SwiftUI final class MyItem: Identifiable, Labelled { var label: String init(_ label: String) { self.label = label } } protocol Labelled { var label: String { get } } struct HelloView: View { let label: String var body: some View { Text(label) } } struct ListView<Element: Labelled & Identifiable>: View { @Binding var elements: [Element] var body: some View { List { ForEach($elements, id: \.id) { $element in HelloView(label: element.label) // crash // Replacing the above with a predefined view works correctly // Text(element.label) } } } } struct ForEachBindingRepro: View { @State var elements: [MyItem] = [ MyItem("hello"), MyItem("world"), ] var body: some View { ListView(elements: $elements) } } #Preview("ForEachBindingRepro") { ForEachBindingRepro() } foreachbindingrepro-2025-07-12-020628.ips
6
0
137
1w
Different toolbar item placement for iPhone vs iPad
On iPhone, I would like to have a more button at the top right of the navigation bar, a search field in the bottom toolbar, and a plus button to the right of the search field. I've achieved this via the code below. But on iPad they should be in the navigation bar at the trailing edge from left to right: plus, more, search field. Just like the Shortcuts app, if there's not enough horizontal space, the search field should collapse into a button, and with even smaller space the search bar should become full-width under the navigation bar. Right now on iPad the search bar is full width under the navigation bar, more at top right, plus at bottom middle, no matter how big the window is. How can I achieve that? Any way to specify them for the system to more automatically do the right thing, or would I need to check specifically for iPhone vs iPad UIDevice to change the code? struct ContentView: View { @State private var searchText = "" var body: some View { NavigationStack { VStack { Text("Hello, world!") } .navigationTitle("Test App") .searchable(text: $searchText) .toolbar { ToolbarItem { Menu { //... } label: { Label("More", systemImage: "ellipsis") } } DefaultToolbarItem(kind: .search, placement: .bottomBar) ToolbarSpacer(.fixed, placement: .bottomBar) ToolbarItem(placement: .bottomBar) { Button { print("Add tapped") } label: { Label("Add", systemImage: "plus") } } } } } }
2
0
56
1w
How to hide scroll edge effect until scroll down
I present a view in a sheet that consists of a navigation stack and a scroll view which has a photo pushed to the top by setting .ignoresSafeArea(edges: .top). The problem is the top of the photo is blurry due to the scroll edge effect. I would like to hide the scroll edge effect so the photo is fully visible when scrolled to the top but let the effect become visible upon scrolling down. Is that possible? struct ContentView: View { @State private var showingSheet = false var body: some View { VStack { Button("Present Sheet") { showingSheet = true } } .sheet(isPresented: $showingSheet) { SheetView() } } } struct SheetView: View { @Environment(\.dismiss) private var dismiss var body: some View { NavigationStack { ScrollView { VStack { Image("Photo") .resizable() .scaledToFill() } } .ignoresSafeArea(edges: .top) .toolbar { ToolbarItem(placement: .cancellationAction) { Button(role: .close) { dismiss() } } ToolbarItem { EditButton() } } } } }
1
0
66
1w
UISearchBar .minimal no background when compiled on Xcode 26
When compiled on Xcode 16.4.0: When compiled on Xcode 26: The code: import SwiftUI struct SearchBarController: UIViewRepresentable { @Binding var text: String var placeholderText: String class Coordinator: NSObject, UISearchBarDelegate { @Binding var text: String init(text: Binding<String>) { _text = text } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { text = searchText } } func makeUIView(context: Context) -> UISearchBar { let searchBar = UISearchBar(frame: .zero) searchBar.delegate = context.coordinator searchBar.placeholder = placeholderText searchBar.searchBarStyle = .minimal return searchBar } func updateUIView(_ uiView: UISearchBar, context: Context) { uiView.text = text } func makeCoordinator() -> SearchBarController.Coordinator { return Coordinator(text: $text) } }
2
0
52
1w
Liquid glass: UIPageViewController inside UITabbarController adding blur effect always in iOS26
When using UIPageViewController inside a UITabBarController on iOS 26 with Liquid Glass adoption, visiting the PageViewController tab applies a blur effect to the navigation bar and tab bar even though the current child view controller of the pageView is not scrollable and does not reach behind these bars. Questions: Is this the expected behavior that the pageview's internal scroll view causes the bars to blur regardless of the page view's child content’s scrollability? If so, is there an official way to make the blur effect appear only when the pageview's current child view controller actually scrolls behind the navigation bar or tab bar, and not in static cases? Tried the same in SwiftUI using TabView and TabView with page style. Facing the same issue there as well. Sample screenshots for reference, Sample SwiftUI code, struct TabContentView: View { var body: some View { TabView { // First Tab: Paging View PagingView() .tabItem { Label("Pages", systemImage: "square.fill.on.square.fill") } // Second Tab: Normal View NavigationStack { ListView() } .tabItem { Label("Second", systemImage: "star.fill") } // Third Tab: Normal View PageView(color: .blue, text: "Page 3") .tabItem { Label("Third", systemImage: "gearshape.fill") } } .ignoresSafeArea() } } struct PagingView: View { var body: some View { TabView { PageView(color: .red, text: "Page 1") PageView(color: .green, text: "Page 2") PageView(color: .blue, text: "Page 3") } .tabViewStyle(.page) // Enables swipe paging .indexViewStyle(.page(backgroundDisplayMode: .always)) .ignoresSafeArea()// Dots indicator } }
0
0
104
1w
fullscreencover Problem
Hello Apple Developer Community: I have a problem with the fullscreencover. I can see the Things, that shouldn’t be visible behind it. I’m currently developing with iOS 26 and only there it happens. I hope you can help me :) Have a nice day
1
3
40
1w
Looking for a mechanism in iPadOS 26 to 'split a window' into two adjacent windows like it worked in iPadOS 18.
With the new multi-windowing design in iPadOS 26, the behavior of openWindow() has changed. In iPadOS 18, if I called openWindow(), I would go from a full-screen window to two side-by-side windows. That allowed my app to meet the goal of the user; keep some information on the screen while being able to navigate through the app in the other window. In iPadOS 26 (beta 3), this no longer works. Instead, a new Window opens ON top of the current window. I am looking for some mechanism to help the user see that two windows are now present and then easily move them on the screen (tiled, side-by-side) or whatever else they would prefer.
1
0
44
1w
Cannot get drop action to trigger (Xcode 26 beta 3)
I'm unable to find the right combination modifiers to get drag and drop to work using the new .draggable(containerItemID:) and dragContainer(for:in:selection:_:) modifiers. The drag is initiated with the item's ID, the item is requested from the .dragContainer modifier, but the drop closure is never triggered. Minimal repro: struct Item: Identifiable, Codable, Transferable { var id = UUID() var value: String static var transferRepresentation: some TransferRepresentation { CodableRepresentation(contentType: .tab) } } struct DragDrop: View { @State var items: [Item] = [ Item(value: "Hello"), Item(value: "world"), Item(value: "something"), Item(value: "else") ] var body: some View { List(items) { item in HStack { Text(item.value) Spacer() } .contentShape(Rectangle()) .draggable(containerItemID: item.id) .dropDestination(for: Item.self) { items, session in print("Drop: \(items)") } } .dragContainer(for: Item.self) { itemID in print("Drag: \(itemID)") return items.filter { itemID == $0.id } } } } #Preview("Simple") { DragDrop() }
4
0
64
14h
coreml Fetching decryption key from server failed
My iOS app supports iOS 18, and I’m using an encrypted CoreML model secured with a key generated from Xcode. Every few months (around every 3 months), the encrypted model fails to load for both me and my users. When I investigate, I find this error: coreml Fetching decryption key from server failed: noEntryFound("No records found"). Make sure the encryption key was generated with correct team ID To temporarily fix it, I delete the old key, generate a new one, re-encrypt the model, and submit an app update. This resolves the issue, but only for a while. This is a terrible experience for users and obviously not a sustainable solution. I want to understand: Why is this happening? Is there a known expiration or invalidation policy for CoreML encryption keys? How can I prevent this issue permanently? Any insights or official guidance would be really appreciated.
5
2
422
1w
List View within a Scrollview
The bane of my existence has been designing interfaces where the whole view needs to scroll, but a portion is a List and the other portion is static. I run into this problem time and again so I was hoping someone has a good solution because we all know that embedding a List view inside ScrollView is a no-go within SwiftUI. It simply doesn't work. So what is a best practice when you need the whole screen to scroll, but a portion is a List? Use a navigation stack instead of a ScrollView? What if it's a child view of a navigation stack already?
Topic: Design SubTopic: General Tags:
2
0
701
1w
PhaseAnimator without transition between phases
PhaseAnimator seems a good fit to play gifs in SwiftUI: struct ContentView: View { let frames = [UIImage(named: "frame-1")!, UIImage(named: "frame-2")!] var body: some View { PhaseAnimator(frames.indices) { index in Image(uiImage: frames[index]) } } } The problem is that by default, there's an opacity transition between phases. So I tried using transition(.identity): Image(uiImage: gif[index]) .transition(.identity) .id(index) It doesn't work. It stays frozen on the first frame. It does work if I set the transition to a small offset value: Image(uiImage: gif[index]) .transition(.offset(x: 0, y: 0.1)) .id(index) It does feel a bit hacky, though. Is this the expected behavior for .transition(.identity), or is it a bug?
1
0
75
1w
How to show confirmationDialog from a Button in a Menu
I have a More button in my nav bar that contains a Delete action, and when you tap that I want to show a confirmation dialog before performing the deletion. In order words, I have a toolbar containing a toolbar item containing a menu containing a button that when tapped needs to show a confirmation dialog. In iOS 26, you're supposed to add the confirmationDialog on the view that presents the action sheet so that it can point to the source view or morph out of it if it's liquid glass. But when I do that, the confirmation dialog does not appear. Is that a bug or am I doing something wrong? struct ContentView: View { @State private var showingDeleteConfirmation = false var body: some View { NavigationStack { Text("👋, 🌎!") .toolbar { ToolbarItem { Menu { Button(role: .destructive) { showingDeleteConfirmation.toggle() } label: { Label("Delete", systemImage: "trash") } .confirmationDialog("Are you sure?", isPresented: $showingDeleteConfirmation) { Button(role: .destructive) { print("Delete it") } label: { Text("Delete") } Button(role: .cancel, action: {}) } } label: { Label("More", systemImage: "ellipsis") } } } } } }
1
0
87
1w
Complex view structures are frustratingly too much work
The Java Swing and AWT MVC model made it easy to develop complex UIs with data interactions that were not described readily in a nested layer that SwiftUI demands. The implicit update model of SwiftUI greatly complicates development of applications that often requires nested components to have to know too much about other components and other structures than their own, because button events and other user interactions cannot readily alter state across layers. A button push on one component then has to be knowledgable about state in other components which have to have that state represented as @State or @Binding etc. and this causes all kinds of wiring to be spread all over the place rather than have a more centralized "state management function" that would be able to look at the world and synchronize the UIs state across changes. The fact that the compiler get's lost in the weeds when types and signatures don't match in deeper component structures doesn't help because it makes it doubly hard to do refactoring to raise and lower state management within the structure readily, because the compiler just cannot simply tell you that a function or constructor signature is no longer correct.
1
0
68
1w
Why is SwiftUI so broken and not improving layered UI functionality
Again and and again, I reach the point in a new application where I need to make structural changes in components and my data model, and the SwiftUI compiler fails to compile and just reports "I'm lost in the weeds", with no indication of what it was last working on, aside from a particular level in a multi-layered nested UI. This typically happens when a sub-views construction is not coded correctly because I changed that view and am looking for what broke, by just letting the compiler tell me what is not compatible. This is how refactoring has been done for ages and it's just amazingly frustrating that Apple engineers don't seem to understand nor care about this issue enough to fix it. Why does this problem persist through version after version of SwiftUI? Is no-one actually using it for anything?
1
0
67
1w
Secure Field "Lags" when certain conditions met.
Hello, I was doing some tasks, and then noticed a small lag/delay when tapping on a Secure field, I tried to investigate it, and noticed this was not my app issue, so I got it into a Playground and the issue is there (Is there in Physical devices, simulator, playground, iPad playground) So I suppose this can be SwiftUI Issue: import SwiftUI struct ContentView: View { @State var field1: String = "" @State var field2: String = "" @State var field3: String = "" var body: some View { VStack { TextField("", text: $field1, prompt: Text("User")) SecureField("", text: $field2, prompt: Text("pass")) SecureField("", text: $field3, prompt: Text("uvv")) } } } So When the focus is set on Field1 TextField, and then you tap the second field, there is a small delay (Even in simulator, there is a small jump trying to show the keyboard, and in an iPad with physical keyboard, the on-screen keyboard is shown). The console only shows this message: Cannot show Automatic Strong Passwords for app bundleID: ... due to error: Cannot save passwords for this app. Make sure you have set up Associated Domains for your app and AutoFill Passwords is enabled in Settings If you change the order of the elements, or some types, this lag disappears. (For example, adding first the SecureField : [SecureField, TextField, SecureField] the Issue disappears.) (Even tried to add textContentType as password, newPassword and emailAddress without helping any bit.
1
0
43
1w
What is the difference between .safeAreaInset and the new .safeAreaBar?
I've been trying out the new .safeAreaBar modifier for iOS 26, but I cannot seem to notice any difference between that and .safeAreaInset? The documentation says: the bar modifier configures the content to support views to automatically extend the edge effect of any scroll view’s the bar adjusts safe area of. But I can't seem to see that in action.
0
0
139
1w
StateObject is not deinitialized when List(selection:) binding
Hello, I have a simple example using StateObject and List. When I bind the List(selection:) to a property of the StateObject like this: List(selection: $viewModel.selectedIndex) { ... } I noticed that each time I push the view using a NavigationLink, a new instance of the StateObject is created. However, when I pop the view, the deinit of the StateObject is not called. When is deinit actually expected to be called in this case? Example code: import SwiftUI @main struct NavigationViewDeinitSampleApp: App { var body: some Scene { WindowGroup { NavigationStack { ContentView() } } } } struct Item: Hashable { let text: String } @MainActor fileprivate class ContentViewModel: ObservableObject { @Published var selectedIndex: Int? = nil init() { NSLog("ContentViewModel.init") } deinit { NSLog("ContentViewModel.deinit") } } struct ContentView: View { @StateObject private var model = ContentViewModel() let items: [Item] = { return (0...10).map { i in Item(text: "\(i)") } }() var body: some View { List(selection: $model.selectedIndex) { ForEach(items.indices, id: \.self) { idx in let item = items[idx] NavigationLink { ContentView() } label: { Text(item.text) } } } } } Interestingly, if I instead use a plain @State variable inside the View: @State private var selectedIndex: Int? ... List(selection: $selectedIndex) { ... } Then the deinit of the StateObject does get called when the view is popped. Because there's no sign of deinit being triggered in the first pattern, I’m starting to suspect this might be a SwiftUI bug. Has anyone seen this behavior or have more information about it? Thanks in advance. Environment: Xcode: 16.4(16F6) iOS Simulator: iPhone SE3 iOS16.4(20E247),iPhone SE3 iOS 18.4(22E238)
1
0
79
1w