Search results for

SwiftUI List performance

50,605 results found

Post

Replies

Boosts

Views

Activity

SwiftUI List .scrollPosition not working
Hi, I am trying to read in which section in a list the user is currently located and want to scroll him to a specific section. Therefore I would like to use the new .scrollPosition modifier. Best would be the ability to implement the same snapping effect from ScrollView. So I used this snippet: struct Item: Identifiable{ var id: Int static func getMany() -> [Item] { var items = [Item]() for i in 0..<100 { items.append(Item(id: i)) } return items } } struct ContentView: View { @State var items = Item.getMany() @State var scrolledID: Item.ID? var body: some View { NavigationStack { List { ForEach(items) { item in ItemView(item: item) } } .scrollTargetLayout() .scrollPosition(id: $scrolledID) .navigationTitle(Hello, (scrolledID ?? 0)) } } } struct ItemView: View { var item: Item var body: some View { Text(Hello world, (item)) } } Doesn't work. So I tried to place the modifiers in different places in the code to attack several different parts of the list as the scrollTargetLayout
2
0
684
Dec ’24
Programmatically navigate from list view in SwiftUI
I have a list of navigation links. Pressing a list item navigates to detail view. So far so good. My issue is that I want to do it programmatically as well. Deep linking a notification for example. It works as long as the list item is visible. But once it is off screen it wont work. So I figured I add a scrollViewReader and then a scrollTo. But scrollto is async and I can't figure out how to wait with navigation until scrollTo is done. Is there some even I can listen too? I'm on iOS 14 if there's any new solution for it there.
0
0
459
Oct ’20
SwiftUI List reuse row content
I noticed that List doesn’t reuse content when scrolling. Is it correct behaviour? I created a wrapper on UILabel and expected List to re use UILabel while scrolling. struct ContentView: View { let items = 0...50 var body: some View { List(items, id: .self) { item in AttributedText(text: (item)) } } } struct AttributedText: UIViewRepresentable, Equatable { let text: String func makeUIView(context: Context) -> UILabel { print(#function, text) return UILabel() } func updateUIView(_ label: UILabel, context: Context) { print(#function, text) label.text = text } }
0
0
799
Dec ’20
SwiftUI app crashes while scrolling section in List via ScrollViewReader
In our SwiftUI application , we are using List in one of our screen. We have a requirement to scroll to specific section header. Below code works fine till iOS16 and from iOS16 onwards we are getting the app crash. Can anyone suggest me any probable solution to achive this ?. ScrollViewReader { proxy in List { ForEach(Array(viewModel.newSections), id: .self) { sectionName in Section(header: VStack { LeaguesScheduleListSectionView(text: sectionName) }) { ForEach(viewModel.newRowsPerSection[sectionName] ?? []) { leagueScheduleEvent in LeaguesScheduleListRowView(leagueSchedule: leagueScheduleEvent) } } .id(sectionName) } } .listStyle(.plain) .onAppear(perform: { if viewModel.newSections.contains(Today/Ongoing), viewModel.newRowsPerSection[(Today/Ongoing)]?.count ?? 0 > 0 { proxy.scrollTo(Today/Ongoing, anchor: UnitPoint.topLeading) } }) }
2
0
1.2k
Mar ’23
Unwanted animation when moving items in SwiftUI list
Hi,I have follow code for create movable List using SwiftUI:struct ContentView: View { @State var numbers = [1, 2, 3] @State var editMode = EditMode.inactive var body: some View { NavigationView { List { ForEach(numbers, id: .self) { number in Text(number) } .onMove { self.numbers.move(fromOffsets: $0, toOffset: $1) } } .navigationBarItems(trailing: EditButton()) .navigationBarTitle(Numbers) } } }But I have strange unwanted animation when move item up in SwiftUI list.I recorded a video that shows the problem:https://www.youtube.com/watch?v=JpVes2nFTMEQuestions:Is this a known problem?Is there a workaround? Did I implement onMove correctly?Using XCode 11.4.1 and the build target is iOS 13.4.1
1
0
2.2k
May ’20
How to take a screenshot of List in a ScrollView of SwiftUI?
I found this way to take screenshot in full screen view of SwiftUI. But How to take screenshot of a ScrollView content view? Thanks! Normal way: SwiftUI Extension extension View { func takeScreenshot(origin: CGPoint, size: CGSize) -> UIImage { let window = UIWindow(frame: CGRect(origin: origin, size: size)) let hosting = UIHostingController(rootView: self) window.rootViewController = hosting window.makeKeyAndVisible() return hosting.view.snapshotImage()! } } Swift Extension extension UIView { func snapshotImage() -> UIImage { ..... } }
3
0
5.8k
Jul ’20
SwiftUI List cell reuse / view lifecycle behavior when scrolling
I’m trying to understand how SwiftUI List handles row lifecycle and reuse during scrolling. I have a list with around 60 card views; on initial load, only about 7 rows are created, but after scrolling to the bottom all rows appear to be created, and when scrolling back to the top I again observe multiple updates and apparent re-creation of rows. I confirmed this behavior using Instruments by profiling my app. Even though each row has a stable identifier, the row views still seem to be destroyed and recreated, which doesn’t resemble UIKit’s cell reuse model. I’d like clarity on how List uses identifiers internally, what actually gets reused versus recreated, and how developers should reason about performance and view lifetime in this case.
0
0
227
Dec ’25
Is List in SwiftUI also lazy loading content in iOS 14 like LazyVStack?
There is LazyVStack to work with a large amount of content inside a ScrollView and ensure good scrolling performance. I like to work with List in my apps as it provides some more functionality than just a ScrollView with a stack of content inside. Does List in SwiftUI now also use lazy loading of content elements in iOS 14? If it does not support this by default is there any way to make it work with lazy loaded content? I tried this code but it caused rendering issues: List { LazyVStack { Text(1) // [...] hundreds of Text views for testing } }
3
0
12k
Jun ’20
SwiftUI, List items reordering issue (iOS 16)
Hello, On iOS 15, I had a feature who allow the user to reorder some items inside of a list. Some view was not allowed to move (I use the modifier .moveDisabled for this behavior). In iOS 15, everything was working fine : I'm able to reorder items inside of the list (only for those who was allowed to be moved). But in iOS 16, I have a strange behavior : I still can reorder items, but if two items are not allowed to be reordered, I can't move any items between them. Here is the example : iOS 15 : iOS 16 : For this feature, I only use: standard SwiftUI List. .environment(.editMode, .constant(.active)) (the view should always be editable). .moveDisabled() to allow items to be moved. No custom element here, everything is SwiftUI framework. What am I doing wrong? Thanks, Alexandre
6
0
3.4k
Sep ’22
Dynamic list sections with observed data with SwiftUI
Hi everyone! I'm currently struggling with dynamically filtering data in a SwiftUi List view. In the following example, I created some example data and stored them within TestArray. These data is dynamically filtered and grouped by the starting letter. The data of the Testclass objects can be changed in EditDetails view. Unfortunately, when changing the name (as it is the relevant property for filtering here), when closing the modal, the user does not return to DetailView but will break the view hierarchy and end up in the ContentView. I assume the issue is the update within ContentView, which is recreating the DetailView stack. Is it possible to ensure the return to view, where the modal has been opened from (DetailView)? Here is some code if you would like to reproduce the issue: import Foundation import SwiftUI import Combine struct ContentView: View { @StateObject var objects = TestArray() var body: some View { NavigationView{ List { ForEach(objects.objectList.compactMa
2
0
2.6k
Jun ’22
Swiftui list row with multiple buttons?
Can one get multiple buttons to work when included in a List row?This problem can be seen in the completed version of the Apple tutorial. There, the HikeDetail or HikeView each work when previewed by itself - one can select different graphs by the Elevation, Heart Rate, and Pace buttons. However, this view does not work properly when it is included in the ProfileSummary List(). When tapping on one of the buttons, the view collapses. This is because all buttons are activated, rather than just the one tapped.It seems that List() is deciding that if it sees a button in the row, it will change multiple behaviors: 1) The button is no longer tinted - if one uses a simple button, it is tinted outside of a List. (Note: The tutorial is playing with colors to show blue/gray - even in the List view). 2) The button no longer flashes when pressed/tapped. 3) A tap in the row activates the actions of all buttons included in that row.I can understand that this might be the desired
22
0
28k
Jul ’19
List selection issue in SwiftUI
After adding an element to an array, the List displaying its data is refreshed, the last element is always selected (according to the sort order) and not the last inserted element, even if I update the selection variable programmatically in the addItem method. To better track the issue, I added an .onChange event handler in which it is possible to observe that after the insertion of a new element, the selection variable changes twice. I prepared a sample project as a variant of the template provided by Xcode for MacOS App with CoreData. You can replace the ContentView of the template with the one below. Thank you. @Environment(.managedObjectContext) private var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: Item.timestamp, ascending: false)], animation: .default) private var items: FetchedResults @State var counterSel:Int = 0 @State var selection:Item? var body: some View { NavigationSplitView { List(Array(items), id:.id, selection: $selection) { item in NavigationLin
1
0
1.1k
Jan ’24