0

Using SwiftUI, I have a List of items retrieved from CoreData. Everything I read makes it look like the list should be something I can scroll normally, but in both the simulator and running on an iPad, the List doesn't scroll.

Clearly there is some piece missing, but I can't seem to find what it is. The list populates correctly, but it simply won't scroll.

struct PeopleList : View {    
    @Environment(\.managedObjectContext) var managedObjectContext
     
     @FetchRequest(
        entity: Person.entity(),
         sortDescriptors: [
            NSSortDescriptor(keyPath: \Person.lastName, ascending: true),           
            NSSortDescriptor(keyPath: \Person.firstName, ascending: true)
         ]
     ) var people: FetchedResults<Person>
    
    var body: some View {
        ZStack(alignment: .topLeading) {
            List(self.people, id: \.self) { person in
                Text(person.descriptionForList())                                             
             }                          
        }
        .frame(width: 400, height: 200)
        .modifier(RoundedEdge(width: 4, color: Color(red: 0.6, green: 0.6, blue: 0.6), cornerRadius: 10))
        .padding(10)
    }
}

Things that didn't change anything:

  • Removing the ZStack { }
  • Replacing the List with a simple List(0...100, id: \.self) { item in Text("hey \(item)") } -- still won't scroll.
  • Adding a .frame() to the List() itself.
  • (added) removing the .frame .modifier and .padding all accomplished nothing.

This is not about (the following topics have Q&A's addressing them, but not this issue):

  • programmatic scrolling
  • disabling scrolling
  • scroll indicators
  • ScrollView
  • UIKit

How can I track down what might be preventing the List() from scrolling?

4
  • 2
    What is the RoundedEdge modifier? Did you try to remove it as well? Commented Jul 9, 2020 at 17:33
  • Recreated without RoundedEdge and scrolling is working for me
    – davidev
    Commented Jul 9, 2020 at 17:36
  • Does Person contain a unique field e.g. id? Did you try using a unique identifier instead of self? e.g. List(self.people, id: \.id) { person in
    – Norman
    Commented Jul 9, 2020 at 18:27
  • Commented out the .modifier(RoundedEdge()), no change. The list populates fine, and when I make the list as simply being numbers 1...100, same behavior, so not thinking it's a matter of the .id either. Maybe an issue with the View where this struct is showing up... Commented Jul 9, 2020 at 19:04

1 Answer 1

5

Turns out, the issue wasn't with anything in the struct that was posed above; it all had to do with the View where that struct was being used. Specifically, when a Color() -- even of .opacity(0) -- was in a ZStack sitting "above" the List(), the latter stops scrolling. Displays fine, but just won't scroll.

This question describes the same thing happening, albeit in somewhat different circumstances.

I'll leave this question up, since somebody else may be at the same place I was, "why isn't my List() scrolling?", rather than "why isn't my List() in a ZStack scrolling?" Hopefully a version of Swift later than 13 will fix this behavior!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.