19

I'm experimenting with iOS 14 widgets and have a weird issue. My widget is not loading when I'm using a List in the widget view. If I'm using HStack, VStack etc. all works ok.

Here is some simple code for the sake of example:

struct WidgetView: View {

   var body: some View {
    
       List {
           Text("Test 1")
           Text("Test 2")
       }
   }
}

I'm seeing this image as a result: enter image description here

Again, it all works well with other elements, this is only happening with List.

pawello2222
  • 46,897
  • 22
  • 145
  • 209
chnski
  • 557
  • 1
  • 4
  • 20

2 Answers2

13

List has a scrolling function not supported on the widget, widgets only display static views, instead try using forEach.

For example:

struct SomeView: View {
    
    var body: some View {
        HStack {
            ForEach(1..<5) { index in
                otherView()
            }
        }
    }
}

You may use an HStack, or VStack depending on your needs and design.

Dharman
  • 30,962
  • 25
  • 85
  • 135
J Arango
  • 939
  • 1
  • 8
  • 21
7

Widgets do not support scrolling, this limitation is talked about in the meet widget video of apple (you can fast forward to 12:02 in the video)

I had the exact same issue and there are two parts to resolve this

  1. Make sure your model confirms to the Identifiable protocol OR at least has one field which acts as the primary key

Example: let's assume you had a structure named Task

   struct Task: Identifiable {
   let title,id:String
   }

and here's how the TimelineEntry structure looks like

struct TaskEntry: TimelineEntry {
let tasks: [Task]
let date: Date
}
  1. Once you do that you can use the ForEach loop to construct your view using an array

    struct ToDoAppWidgetEntryView : View {
    var entry: Provider.Entry
    
    var body: some View {
     VStack{
         ForEach(entry.tasks, id:\.id){
             item in
             Text(item.title)
             Divider()
         }
        }
       }
    }
    

This is how the preview looks like

enter image description here

Bug Hunter Zoro
  • 1,743
  • 18
  • 21