SwiftUI List performance

39,832 results found

Post marked as solved
1 Replies
591 Views
I am building a list in SwiftUI. In my view, I want the 'Jesse Lingard' and 'Manchester United' text to be next to the image but they are appearing underneath. How do I fix this?
Posted
by
Post not yet marked as solved
2 Replies
3.9k Views
I am working with a ScrollView and a LazyVStack that can have many rows, sometimes 500 or more. My rows are expandable on tap. Performance of expand/collapse is unacceptable when the number of rows exceeds 100. The more rows, the more performance degrades. This is the gist of my code: struct PlaceView: View { tt/* ... */ ttvar place: Place tt@Binding expandedPlaceId: Int ttvar content: some View { ttttVStack(alignment: .leading) { ttttttText(place.name) tttttt ttttttif self.expandedPlaceId.wrappedValue == place.id { ttttttttVStack(alignment: .leading) { ttttttttttText(place.city) tttttttt} tttttt} tttt} tt} } struct PlacesListView: View { tt/*...*/ tt@State expandedPlaceId = -1 ttvar body: some View { tttScrollView { ttttLazyVStack { tttttForEach(places, id: .id) { place in ttttttPlaceView(place: place, expandedPlaceId: self.$expandedPlaceId) tttttttt.onTapGesture { self.selectDeselect(place) } tttttttt.animation(.linear(duration: 0.3)) ttttt} ttt } tt } t} } I created an init function in Pl
Posted
by
Post marked as solved
2 Replies
3.5k Views
I have isolated this very simple piece of code. Basically I get a data representation of an image from Core Data and display it in SwiftUI using Image. The problem is, it's trying to load the image again every time you scroll the scroll view, which leads to a performance issue. The part that I do not understand is, that this only happens while the Image is wrapped in a GeometryReader and only when reader.frame is accessed somewhere like in the Text object. If I comment out that Text object, everything is smooth and the image is only loaded once. Is this a bug or am I misunderstanding something here?var backgroundImage: UIImage { print(LOAD IMAGE) if let imageData = gameLocal.backgroundImage { return UIImage(data: imageData)! } else { return Constants.placeholderImage } } var body: some View { ScrollView { GeometryReader { reader in Image(uiImage: self.backgroundImage) .resizable() .foregroundColor(Color.gray) // Text(Offset.y: (reader.frame(in: .global).minY)) } .frame(minWidth: 0, maxWidth:
Posted
by
Post not yet marked as solved
1 Replies
745 Views
Hi, I'm having a hard time trying to solve this impossibly small problem in SwiftUI. I have a list like this: struct ContentView: View { var body: some View { List { Button(action: { print(hello) }, label: { Text(Hello) }) } } } And every time I tap on it, the row deselect without animation, but I want the same effect as UITableView.deselectRow(at:animated:). Why is this so hard or am I missing something? I don't want to use NavigationLink because I only want to execute code upon tap, not navigating to other view, unless it's possible to only execute code using NavigationLink... Thanks,
Posted
by
Post marked as solved
4 Replies
5.0k Views
Hello, I am creating a macOS app with SwiftUI and I added a sidebar with NavigationView and inside I added a List with NavigationLinks. I also added a separate NavigationLink at the bottom of the Window. The behavior I want to have is, when I press the NavigationLink at the bottom, then to deselect all the other NavigationLinks in the sidebar List. Most probably I still haven't properly understood SwiftUI and it's declarative paradigm yet, and I can't see a way to do this. Could someone help me? This is the code I have written: Enum enum Menu: String, CaseIterable { case library = Library case catalogue = Catalogue case filter = Filter } Main Code struct SplitView: View { var body: some View { NavigationView { VStack(alignment: .leading, spacing: nil) { MenuList() Divider() Spacer() NavigationLink(destination: FilterSpace()) { HStack { Text(􀌈) Text(Filter) } .padding(5.0) .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color.gray, lineWidth: 1)) } .cornerRadius(10) .pad
Posted
by
Post not yet marked as solved
9 Replies
11k Views
<body><p>I've got a MacOS app and I want to be able to select a particular item in a List. Here's an outline of what I'm trying to do:import SwiftUIstruct SomeObject : Identifiable { let id = UUID() let name: String // lots of other stuff}struct ContentView: View { var someObjects: [SomeObject] @Binding var selectedObject: SomeObject var body: some View { List(someObjects, selection: $selectedObject) { someObject in Text(someObject.name) } } init(_ someObjects: [SomeObject]) { self.someObjects = someObjects }}I get the error Type of expression is ambiguous without more context on the Text initializer, with the n in someObject.name underlined in red.What does this error mean? If I remove the selection: from the List initializer and take out the @Binding line, it compiles fine.
Posted
by
Post not yet marked as solved
0 Replies
248 Views
Does anyone have some resources to recommend for learning more about measuring and optimizing SwiftUI performance and rendering? I haven't seen a ton from Apple that looks like it's going to help me more. I did find two sessions from WWDC 2023. The Demystify SwiftUI performance session[1] does tell us the _printChanges function can help tell us when component bodies are computed. I see how I can attach this to a component that I built… I'm not (yet) sure if I have the ability to run this on arbitrary system components in my component graph. The Analyze hangs with Instruments session[2] does walk us through using the SwiftUI Instruments to measure body computations. Does anyone have any more resources along these lines for learning more? SwiftUI is closed source… so we don't really have easy visibility inside to see for ourselves. Bummer. Are there any additional debug APIs (like _printChanges) that can help for local development? I'm also interested to lea
Posted
by
Post marked as solved
3 Replies
4.8k Views
I get a crash when reloading a List based on some search results. The results may change out the sections and rows. The error returned looks like an internal error because sometimes it works and sometimes it crashes on the same search string.Error:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempt to create two animations for cell' *** First throw call stack: ( 0 CoreFoundation 0x00007fff23afdbde __exceptionPreprocess + 350 1 libobjc.A.dylib 0x00007fff5015cb20 objc_exception_throw + 48 2 CoreFoundation 0x00007fff23afd958 +[NSException raise:format:arguments:] + 88 3 Foundation 0x00007fff255506f5 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191 4 UIKitCore 0x00007fff47134191 -[_UITableViewUpdateSupport(Private) _setupAnimationsForNewlyInsertedCells] + 1575 5 UIKitCore 0x00007fff4713ef73 -[_UITableViewUpdateSupport _setupAnimations] + 118 6 UIKitCore 0x00007fff470d5ec4 -[UITableView _updateWithItems:updateSupport:] + 2883
Posted
by
Post marked as solved
6 Replies
723 Views
Hi there, I am still quite new to Swift. Is there a easy way to search a list with navigation links to other swift pages? Thank you so much for your help! Laurin
Posted
by
Post marked as solved
4 Replies
284 Views
Hello, I am unable to move multiple rows in a list, it only allows for one at a time. However, I am able to select multiple rows. Here is my code: import SwiftUI struct ContentView: View { @State var items = [Test 1, Test 2, Test 3, Test 4, Test 5, Test 6] @State var selectedItems: Set = .init() var body: some View { NavigationView { List(selection: $selectedItems) { ForEach(items, id: .self) { item in Text(item).tag(item) } .onMove(perform: { indices, newOffset in withAnimation { self.items.move(fromOffsets: indices, toOffset: newOffset) } }) } #if os(iOS) .navigationBarItems(trailing: EditButton()) #endif } .padding() } } I need all the selected rows to move when dragged. Any help would be greatly appreciated, I have tried, and can not find any way to do it. Thanks, Dev_101
Posted
by
Post not yet marked as solved
2 Replies
3.7k Views
I have a use case where I want to navigate to a destination view but also perform an event when the user interacts. Outside of a List this code seems to work: NavigationLink(destination: MyDestinationView()) { HStack { /* content here */ } } .simultaneousGesture(TapGesture().onEnded { /* my action */ }) However, once you put that in a List either the navigation or the action, not both, will fire. Typically tapping anywhere in the HStack will navigate, but tapping on the Text objects will fire the action and not navigate. Is this an issue with List or is there an intended way of doing this that I am not seeing? I also messed with contentShape and it ended up making the HStack fire the action and only the very edges of the cell would navigate. Like the rowItem could only handle 1 gesture event at a time. I tried something like in the ForEach creating a view that had a body of: (view also had a state var of shouldNavigate) Group { NavigationLink(destination: MyView(), isActive
Posted
by
Post not yet marked as solved
8 Replies
8k Views
In SwiftUI 3 we used the following code snippet to set the background color of a list. struct ContentView: View { var body: some View { List { Text(Hello World!) } .onAppear { // Placed here for sample purposes, normally set globally UITableView.appearance().backgroundColor = .clear } .listStyle(.insetGrouped) .background(Color.red) } } This is not working anymore in SwiftUI 4 (FB10225556) as the List is not based on UITableView anymore. Any workaround ideas?
Posted
by
Post not yet marked as solved
1 Replies
1k Views
Hi, I managed to have a nested list in swiftui 3 (with foreach) and drag and drop to reorder the list when I enter the built-in edit mode. Problems / limitation when dragging / dropping a list item from edit mode: I can't change its nesting level: for example, if I drag an item that is 3 nested levels deep, I can't seem to be able to drop it on let's say level 1. When an item has children, and they are expanded, if I drag the father, the children will not follow. I can't drag from one section, and drop on another. What are the solutions to circumvent those problems? Is there built-in way to do it, if not, is it even possible without resorting to UIKit?
Posted
by
Post not yet marked as solved
1 Replies
861 Views
In the following code, click/touch anywhere on any row of the list deletes that row. This is not expected. Only the click/touch on the xmark button should delete that row. import SwiftUI struct MainView: View { private let arr: [String] = [one, two, three] var body: some View { List(arr, id: .self) {item in SecView(name: item) } } } struct SecView: View { var name: String @State private var show = true var body: some View { if show { HStack { Text(name) Button(action: { show = false }, label: { Image(systemName: xmark) }) .border(.yellow) } } } } @main struct XApp: App { var body: some Scene { WindowGroup { MainView() } } } Please check. I ran the above code for iPAD air (5th generation).
Posted
by
Post not yet marked as solved
1 Replies
527 Views
I'm working with a SwiftUI view that contains a Map with numerous Annotations (ranging from 30 to 100). Alongside these, there's a specific annotation that changes based on the user's selection from a list. The list selection might change rapidly, possibly up to 10 times a second. When the selected annotation is updated, it seems that all the annotations on the map view get reloaded. This results in high CPU usage and a significant drop in screen refresh rate, especially during rapid selection changes. How can I optimize this to only reload the selected annotation in the map view instead of all annotations when the selectedAnnotation is changed? Here's the relevant pseudo code: @State var annotations: [MKAnnotation]() @State var selectedAnnotation: MKAnnotation? Map { ForEach(annotations.indices, id: .self) { idx in let anno = annotations[idx] Annotation(, coordinate: anno.coordinate) { Circle() .stroke(.black, lineWidth: 4) .fill(.white) .frame(width: 8, height: 8) } } if let selec
Posted
by