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

SwiftUI Documentation

Post

Replies

Boosts

Views

Activity

iPad app now crashing on Mac. layoutSubtreeIfNeeded ?
My ipAd app is now crashing when being run on a Mac (with designed for iPad emulator). It. has been working a month ago, today was first time I tried in awhile. I get a warning "It's not legal to call -layoutSubtreeIfNeeded on a view which is already being laid out. If you are implementing the view's -layout method, you can call -[super layout] instead. Break on void _NSDetectedLayoutRecursion(void) to debug. This will be logged only once. This may break in the future." shortly after that the App Crashes. Thread 1: EXC_BAD_ACCESS (code=1, address=0x14a0839653d0) The crash happens when my main view (in a Tab view) goes to a DetailView. I stripped out all functionality in my code and it still happens mainView NavigationLink{ ImageDetailView2 () } label: { img } struct ImageDetailView2: View { var body: some View { Text("Hello, World!") } } Any help would be appreciated
3
0
203
Dec ’24
Error: Value of type 'SettingsView' has no member 'tabItem'
I'm going through the tutorial and have copied the code given to me verbatim, but I am still getting the error 'Value of type 'SettingsView' has no member 'tabItem'. Not sure what to do. Code is below. import SwiftUI @main struct MyApp: App { var body: some Scene { WindowGroup { TabView { ContentView() .tabItem { Label("Journal", systemImage: "book") } SettingsView() .tabItem { *error is here - Value of type 'SettingsView' has no member 'tabItem' Label("Settings", systemImage: "gear") } } } } }
1
0
181
Dec ’24
How to update data in a DataFrame
My project loads a CSV into a DataFrame and displays it in a Table (a MacOS app). So far so good ... but when trying to update a value in a column, I dont see anyway to update this value. The table gets the value for the column like this: func getColumnValue(row :DataFrame.Rows.Element, columnName :String) -> String { if row.base.containsColumn(columnName) { var value = "" if row[columnName] != nil { value = "\(row[columnName]!)" } return value } ... But the documentation and googles dont show any way to update the same column. Any help is appreciated with cookies. Attempt to update: func setColumnValue(row :DataFrame.Rows.Element, columnName :String, value :String) { var column: [String?] = data[columnName] column[row.id] = value ... }
6
0
289
Nov ’24
Getting multi-stage text input status in SwiftUI
Hi all, Is there way to check the status of multi-stage text input in the TextField of SwiftUI? I am essentially trying to detect if the user is entering some text with multi-stage text input method right now. (e.g., Japanese, Chinese, etc). TextField("Search", text: $searchText) .onKeyPress(keys: [.upArrow, .downArrow]) { event in if ....some condition here... { // ignore up/down keys when multi-stage input return .ignored } else { // do some special key handling // when multi-stage input is not running return .handled } } Multi-stage text input uses up/down keys to choose words from candidates, so I don't want to handle the keys when it is happening.
0
0
136
Nov ’24
Swift Charts legend overflowing outside frame
I'm building an app that lets users create charts with custom values and while testing, I came up with this bug that happens when the view width forces the legend to have more than 1 line. Due to Swift trying to align the different labels vertically, it's forcing the first line to overflow. Here's the code to generate this: import SwiftUI import Charts struct DonutView: View { var countries:[(country:String, count:Int)] = [ (country: "Africa", count: 54), (country: "Asia", count: 48), (country: "Europe", count: 44), (country: "North America", count: 23), (country: "Oceania", count: 14), (country: "South America", count: 12), ] var body: some View { Chart { ForEach(0..<countries.count, id:\.self) { idx in SectorMark( angle: .value(countries[idx].country, countries[idx].count), innerRadius: .ratio(0.5) ) .foregroundStyle(by: .value("label", countries[idx].country)) } } .frame(width: 310, height: 270) .border(.blue, width: 1) } } Has anyone seen this? Is there a solution? I know about building custom legends, but SwiftUI has no wrapping HStack nor VStack, and the app allows users to change the legend position from the bottom to the top or sides. If I were to go this way, I'd have to write a very large chunk of code to bypass what seems to be a bug.
1
0
201
Nov ’24
Carousel Flicker
Hi, I've created an carousel using an Tabview component and it is divided in pages, each page has 7 items. Now we wan't to load the carousel page by page, but whenever I updated the list which Tabview uses to render, it flickers because it rerenders previous elements. I've tried to use a scroll view for this but this app is for Vision Pro and I added some 3D transformations (position/rotation) to the cards from the tabview and when I added this in the scrollview they becom unclickable. Do you have any sugestions what can I do? Bellow is a sample of the code, I put [items] just to suggest there is a list. VStack (spacing: 45) { TabView(selection: $navigationModel.carouselSelectedPage) { let orderedArtist = [items] let numberOfPages = Int((Double(orderedArtist.count) / Double(cardsPerPage)).rounded(.up)) if(numberOfPages != 1){ Spacer().tag(-1) } ForEach(0..<numberOfPages, id: \.self) { page in LazyHStack(alignment: .top, spacing: 16){ ForEach(0..<cardsPerPage, id: \.self) { index in if(page * cardsPerPage + index < orderedArtist.count){ let item = orderedArtist[page * cardsPerPage + index] GeometryReader{proxy in
1
0
193
Nov ’24
Broken focus on TabView with more than 7 tabs on tvos18
I am using the TabView control in SwiftUI on a tvOS 18 target with the style of sidebarAdaptable. When I have 7 or less tabs things operate correctly. When I add an 8th tab and you navigate to the contents of the tab the menu collapses as expected but you cannot navigate back to restore the menu. This code reproduces the issue: import SwiftUI struct ContentView: View { var body: some View { TabView { Tab("Tab1", systemImage: "person.circle") { Button("Button1") {} } Tab("Tab2", systemImage: "person.circle") { Button("Button2") {} } Tab("Tab3", systemImage: "person.circle") { Button("Button3") {} } Tab("Tab4", systemImage: "person.circle") { Button("Button4") {} } Tab("Tab5", systemImage: "person.circle") { Button("Button5") {} } Tab("Tab6", systemImage: "person.circle") { Button("Button6") {} } Tab("Tab7", systemImage: "person.circle") { Button("Button7") {} } Tab("Tab8", systemImage: "person.circle") { Button("Button8") {} } } .tabViewStyle(.sidebarAdaptable) } } If you eliminate Tab 8 the problem goes away. You can navigate back to the menu by moving to the left. Notably the Destination Video sample also reproduces this issue: https://developer.apple.com/documentation/visionos/destination-video To reproduce: Load the above code or Destination Video sample. Navigate right (click right on remote or swipe right). Menu dismisses and just shows the item you have selected in the upper left corner. Try to navigate left by clicking left on the remote or swiping left. It looks like the collapsed menu gets focus briefly, screen elements flash but the focus remains outside the menu. Has anyone else encountered this issue?
3
3
186
Nov ’24
Infinite view loop
If I add items at the root level, this code works, but if I attempt to add a child to any item, it runs an infinite loop where it goes from the AddItemView back to the SubItemView and starts all over. I suspect it has something to do with the Predicate in SubItemView, but the debugger is crap, so I'm just guessing. Minimal code: @Model final class Item { var id: String var name: String var children: [Item] = [] @Relationship(deleteRule: .cascade) var parent: Item? init(name: String, parent: Item?, id: String = UUID().uuidString) { self.name = name self.parent = parent self.id = id } } struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query( filter: #Predicate<Item> { item in item.parent == nil }, sort: \Item.name ) public var rootItems: [Item] var body: some View { NavigationStack { List { ForEach(rootItems) { item in HStack { NavigationLink ( destination: SubItemView(parent: item)) { Text(item.name) } } } } .toolbar { ToolbarItem(placement: .navigationBarTrailing) { EditButton() } ToolbarItem { NavigationLink(destination: AddItemView(itemParent: nil)) { Text("Add To Do") } } } } } } struct SubItemView: View { @Environment(\.dismiss) private var dismiss @Environment(\.modelContext) private var modelContext @State var parent: Item @State private var todo: String = "" @State var selectedDate = Date() @State var showPicker: Bool = false @Query var children: [Item] init(parent: Item) { self.parent = parent let parentID = parent.id _children = Query(filter: #Predicate<Item> { $0.parent.flatMap(\.id) == parentID && $0.parent.flatMap(\.id) != nil }, sort: \Item.name ) } var body: some View { Form { LabeledContent { TextField("Name", text: $parent.name) } label: { Text("Name:") } } Text("Parent: \(parent.name)\n") NavigationStack { Text("Child count: \(children.count)") List(children) { child in HStack { if(child.children.isEmpty) { Text(child.name) NavigationLink ( destination: SubItemView(parent: child)) { Text("").foregroundColor(.white).background(Color.blue) } .opacity(0) .background( Text("") ) } else { Text(child.name) NavigationLink(destination: SubItemView(parent: child)) { Text("") } } } } } .navigationTitle("Sub Items") .toolbar { ToolbarItem(placement: .navigationBarTrailing) { EditButton() } ToolbarItem { NavigationLink(destination: AddItemView(itemParent: parent)) { Text("Add To Do") } } ToolbarItem { Button("Save") { try? modelContext.save() dismiss() } } } } } struct AddItemView: View { @Environment(\.dismiss) private var dismiss @Environment(\.modelContext) private var context @State var itemParent: Item? @State private var name = "" @State private var showWarning: Bool = false @State var child = Item(name: "", parent: nil) var body: some View { NavigationStack { Form { LabeledContent { TextField("Item", text: $name) } label: { Text("Todo:") } } .navigationTitle("Add New Item") .toolbar { Button("Save") { let tmp = Item(name: name, parent: itemParent) if(itemParent != nil) { itemParent!.children.append(tmp) } context.insert(tmp) try? context.save() dismiss() } } } } }
4
0
252
Nov ’24
SwiftUI View incorrectly disabled based on position?
My app has a sort of "HUD" that sits at the top of the screen. It contains some general information about gameplay, and a couple of interactive elements: a scrolling list of text and a button. It's imbedded into the main view like this: struct WindowedInGameView: View { @Environment(GameModel.self) private var gameModel var body: some View { ZStack(alignment: .top) { VStack { HUDTopView() switch(gameModel.game?.location.player_location) { case .plaza: PlazaView() case .city_hall: CityHallView() ///... Lots of additional views... default: Text("Player is in unknown location.") } // --> Position 2 } if gameModel.showConversationView { ... an overlay for NPC conversations... } if (gameModel.showNotificationView) { .. a notification overlay... } } } } If HUDTopView() is at the indicated place at the top of the VStack, none of its interactive elements work: you can't press buttons, you can't scroll the text. Everything looks present and interactive, it just doesn't seem to receive any input. If I move HUDTopView() to the bottom of the VStack (where the comment indicates "Position 2") it works just fine, except it's not where I want it to be located. So far as I can tell, its position alone is disabling it somehow. In either case, any interactive elements in the views from the switch are acting fine, regardless of their position relative to the HUD view. This fails in the same way on iOS, iPadOS, and macOS. (The visionOS implementation doesn't use this main view, so I haven't tried it there). What it isn't: the overlays at the bottom. Commenting them out doesn't change behaviour. the ZStack - I can replace it with an HStack, and it still fails. Anything about the container of this view -- it fails in preview Anything about the HUDTopView() -- a simple button placed there becomes noninteractive, too. I'm at a complete loss for what could possibly be causing this.
1
0
192
Nov ’24
How to do content transition when one image is a system symbol and the other is in assets catalouge?
I have a save button: Button(action: { saveTapped() }) { Label("Save", systemImage: wasSaved ? "checkmark" : "square.and.arrow.down") } .contentTransition(.symbolEffect(.replace)) .disabled(wasSaved) Now, I created a new custom icon and added it into the assets catalogue: "custom.square.and.arrow.down.badge.checkmark" for when the wasSaved state is true. The issue I am facing is that to use it I need to use the method of Label with image:, while to use the non custom symbol, I need to use systemImage:. So how is it possible to transition between the two?
1
0
203
Nov ’24
SwiftUI Popover Crash During Resizing in Stage Manager
SwiftUI Popover Crash on iPad During Resizing in Stage Manager with Exception. *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Trying to layout popover in the delegate callback popoverPresentationController:willRepositionPopoverToRect:inView: will lead to recursion. Do not force the popover's container view or an ancestor to layout in this callback.' (Occurred from iPadOS 18.1) struct ContentView: View { @State var showPopover: Bool = false var body: some View { VStack { Text("Hello, world!") Button(action: { showPopover = true }, label: { Text("Open Popover") }) } .padding() .popover(isPresented: $showPopover, attachmentAnchor: .point(.trailing), content: { VStack { Text("Popover Content") } }) } }
7
6
439
Nov ’24
Issue with NavigationStack, TabView and List
Hi, I'm experiencing a layout issue in SwiftUI when using TabView, NavigationStack, and a List in a specific navigation flow. Here's how to reproduce it: Tap on Tab 3. Tap "Tap Me to go to the subview" to navigate to the detail view. In the detail view, tap Back to return to Tab 3. Tap "Tap Me to go to the subview" again. Problem: When returning to the detail view, the content (especially the List) appears incorrectly positioned within the safe area, as if it's overlapping or misaligned with the navigation bar. This happens only when navigating back and forth after dismissing a sheet presented from a different tab. This animated gif shows the workflow to visualize the problem: Expected Behavior: The content should consistently respect the safe area and be positioned correctly under the navigation bar. Thanks in advance for your help! Here is the complete code to reproduce the issue: import SwiftUI @Observable class MenuSelector { var initialIndex: Int var customTabIndex: Int var isCustomTabSelected: Bool = false private var previousIndex: Int init(customTabIndex: Int, initialIndex: Int = 0) { self.initialIndex = initialIndex self.customTabIndex = customTabIndex self.itemSelected = initialIndex self.previousIndex = initialIndex } var itemSelected: Int { didSet { if itemSelected == customTabIndex { previousIndex = oldValue itemSelected = oldValue isCustomTabSelected = true } } } } struct NavigationStackSpikeView: View { @State private var tabSelector = MenuSelector(customTabIndex: 1) var body: some View { TabView(selection: $tabSelector.itemSelected) { Text("Tab 1") .tabItem { Text("Tab 1") } .tag(0) Text("I want to present a sheet for this tab") .tabItem { Text("Action") } .tag(1) Tab3View() .tabItem { Text("Tab 3") } .tag(2) } .sheet(isPresented: $tabSelector.isCustomTabSelected) { SheetView() } } } struct Tab3View: View { @State private var navigationPath = NavigationPath() var body: some View { NavigationStack(path: $navigationPath) { VStack { NavigationLink(value: Destination.subview) { Text("Tap Me to go to the subview") } } .navigationDestination(for: Destination.self) { destination in switch destination { case .subview: DetailView() } } } } enum Destination: Hashable { case subview } } struct DetailView: View { var body: some View { VStack { List { Text("A detail") } .listStyle(.plain) } .navigationTitle("Details") .navigationBarTitleDisplayMode(.inline) } } struct SheetView: View { @Environment(\.dismiss) var dismiss var body: some View { VStack { Text("I'm a sheet") Button("Dismiss") { dismiss() } } } } #Preview { NavigationStackSpikeView() }
0
0
167
Nov ’24
How can I make an "inverted" list (bottom to top) with SwiftUI?
I'm trying to figure out how to make an inverted list in my watchOS app for a message view, so that messages appear from the bottom first, and go up. Everything I've tried so far has some sort of major drawback, and I'm wondering if there's some proper way to do it. My current implementation is flipping every message item upside-down, then flipping the whole list upside-down. This works in making the list go from bottom to top, but the digital crown scroll direction is also inverted. Simply inverting the array of messages doesn't work either, as the user has to scroll to the bottom of the list manually every time. Any tips/suggestions would be greatly appreciated.
0
0
224
Nov ’24
Linked list?
The code below works, in that it successfully creates an item and attached it to the parent item, but the parent view doesn't show the new child until you navigate away from the parent and then back to it. Parent: Child1 Child2 // Add Child 3 and this does not refresh when returning through the dismiss() call, but if I navigate to the grand Parent, and then back to the Parent, Child 3 is there. Any ideas? NavigationStack { Form { LabeledContent { TextField("Item", text: $name) } label: { Text("Item:") } } .navigationTitle("Add New Item") .toolbar { Button("Save") { var tmp = Item(timestamp: Date(), name: name, parent: itemParent) if(itemParent != nil) { itemParent!.children.append(tmp) } context.insert(tmp) try? context.save() dismiss() } } }
2
0
304
Nov ’24
searchable issue on iOS 18
Starting with iOS 18, the behavior of searchable and searchSuggestions differs from previous versions. In iOS 17.5, searchSuggestions remained visible even after selecting an item and navigating away. However, in iOS 18, searchSuggestions are dismissed after navigation. Is there a way to keep searchSuggestions visible after navigation, as in iOS 17.5? struct ContentView: View { @State private var query = "" var body: some View { NavigationStack { Color.red .searchable(text: $query) .searchSuggestions { NavigationLink("Element") { Color.blue } } } } } iOS 18.1 iOS 17.5
1
0
175
Nov ’24
Bug fix
Hello, I have these two errors in this particular block of code: Capture of 'self' with non-sendable type 'MusicPlayer?' in a @Sendable closure and Capture of 'localUpdateProgress' with non-sendable type '(Double, Double) -&amp;gt; Void' in a @Sendable closure ` @MainActor func startProgressTimer(updateProgress: @escaping (Double, Double) -&amp;gt; Void) { timer?.invalidate() // Stop any existing timer let localUpdateProgress = updateProgress timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in guard let self = self, let audioPlayer = self.audioPlayer, let currentItem = audioPlayer.currentItem else { print("currentItem is nil or audioPlayer is unavailable") return } let currentTime = currentItem.currentTime().seconds let duration = currentItem.duration.seconds localUpdateProgress(currentTime, duration) } }` I've tried nearly every solution and can't think of one that works. Any help is greatly appreciated :)
1
0
226
Nov ’24
How to use the mouse to select List data items
I have a question about SwiftUI and would like to ask you guys The problem is described as follows I am learning to use SwiftUI. I have a file list display window. I encapsulate the files into a structure called FileInfo and use List to display the files obtained through filemanager. I want to select files by mouse selection. But I didn't find SwiftUI support for selection, so I used Zstack to select, but I found that it is difficult to respond to selection and click events at the same time. Below is my code ZStack { List(selection: $localWorkspaceViewModel.selectedFiles) { ForEach(localWorkspaceViewModel.files.indices, id: \.self) { index in ZStack { } .contextMenu{ // ... } .onTapGesture(count: 2) { // ... } .onTapGesture(count: 1) { // ... } .onDrag { // ... } } .listRowInsets(EdgeInsets()) .listRowSeparator(.hidden) } .contextMenu { // ... } .onDrop(of: ["public.data"], isTargeted: nil) { providers in // ... } Color.clear .contentShape(Rectangle()) .gesture( DragGesture(minimumDistance: 20) .onChanged { value in print() if !isDragging { dragStartPoint = value.startLocation isDragging = true } // ... } .onEnded { _ in isDragging = false } ) .allowsHitTesting(!isDragging) if isDragging { Rectangle() .fill(Color.blue.opacity(0.2)) .frame(width: selectionRect.width, height: selectionRect.height) .position(x: selectionRect.midX, y: selectionRect.midY) .overlay( Rectangle() .stroke(Color.blue, lineWidth: 1) ) .allowsHitTesting(false) } } The code problem is described as follows When the line of code .allowsHitTesting(!isDragging) sets the static value to true or false, the click and drag event of the List item behaves normally or the box selection of Color.clear behaves normally, but only one of them behaves normally. However, after setting it to !isDragging, only one of them behaves normally. I would like to ask how to solve this problem. I would be very grateful if you can give me a solution.
0
0
158
Nov ’24
SwiftUI and dragging a file onto the app icon
I'm playing around with using an app to automate some of my personal work flows, and one of the things I wanted to do was to be able to drag a .webloc file onto my app icon in the dock, to launch it. I've got public.data set up as a document type for it in Xcode, which translated to &lt;key&gt;CFBundleDocumentTypes&lt;/key&gt; &lt;array&gt; &lt;dict&gt; &lt;key&gt;CFBundleTypeRole&lt;/key&gt; &lt;string&gt;Viewer&lt;/string&gt; &lt;key&gt;LSHandlerRank&lt;/key&gt; &lt;string&gt;Default&lt;/string&gt; &lt;key&gt;LSItemContentTypes&lt;/key&gt; &lt;array&gt; &lt;string&gt;public.data&lt;/string&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/array&gt; in the Info.plist for it, which seems correct. When I drag a .webloc file onto the Dock icon, it appears to be willing to accept it, but nothing seems to happen. In the app, I've got an AppDelegate.swift file which has extension Notification.Name { static let receivedURLsNotification = Notification.Name("ReceivedURLsNotification") } class AppDelegate: NSObject, NSApplicationDelegate { func application(_ application: NSApplication, open urls: [URL]) { guard !urls.isEmpty else { return } NotificationCenter.default.post(name: .receivedURLsNotification, object: nil, userInfo: ["URLs": urls]) } } (I copied it almost verbatim from a Medium post.) In the app swift file, I have @main struct LoggerApp: App, DropDelegate { @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate I set a breakpoint on application(_:NSApplication, open:[URL]), and did my drag, and the breakpoint never triggered. I added the application(didFinishLaunching(_:Notification) method, and that does get invoked when the app launches, so the app delegate does seem to be working. That seems to indicate the problem is somewhere else?
3
0
238
Nov ’24