I've created a project in xcode 12 and added a Widget Extension target. The target is created and I'm left with a simple widget that shows the time. The preview of the widget works as expected and the widget is shown as expected in the simulator. But if I change the view to use List
instead of Text
, like this:
struct SimpleWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
List {
Text("Something")
}
}
}
The preview and the simulator both show a red No Entry sign on a yellow background:
I've tried putting the List
inside a VStack
and using a dynamic list without any success. I can't find any documentation about List
being prohibited in a widget. I've tried restarting xcode and the simulator, cleaning and rebuilding. I'm using xcode 12.0 beta 3.
~I also get this issue trying to use a UIView
wrapped in UIViewRepresentable
. The view is a LineChartView
from the Charts library.~ Edit: according to this post on the Apple developer forums it isn't possible to use UIKit views in a WidgetKit widget.
Does anyone know how I can use a List
in a widget? Any help much appreciated. Thanks
Widget Views are static.
They don't support animation, navigation nor user interaction (with the exception of Link
). And they must be written in pure SwiftUI.
Note that the list is not exhaustive, provided mostly with examples:
Interactive views (ones you can interact with, e.g., by touching them):
List
Form
ScrollView
TabView
Map
Picker
Slider
Animated views:
ProgressView
Navigation views (you can't use the standard navigation - for this you have Link
):
NavigationView
NavigationLink
UIKit Wrappers for UIKit Views/ViewControllers:
UIViewRepresentable
UIViewControllerRepresentable
If you want to use any of the above views, e.g. an interactive view like List
, you need to create it out of non-interactive views - here VStack
+ ForEach
.
I got this yellow no entry (ghostbusters without the ghost lol) sign after trying to include a chart from the Charts library (subclass of UIView) using a UIViewRepresentable wrapper. I couldn't find the yellow no entry sign documented anywhere and wasn't sure if it originated from the Charts library or iOS itself so it was a relief to see that you saw the same issue and this set me on the right path to coming up with a solution.
From this post that you referenced above, an Apple Engineer says "UIKit views wrapped in UIViewRepresentable will not work in WidgetKit. When the views are encoded from your extension to be displayed they will appear blank.". I don't think that is entirely accurate. I think it will appear yellow with a red "no entry" sign.
https://developer.apple.com/forums/thread/653471?answerId=619627022#619627022
I got around this issue by rendering my chart as a UIImage and displaying the image on the widget instead of the UIView version of the chart. That should suffice for my purposes as I don't need it to be interactive like I do in the app.
Use ForEach inside a VStack:
var body: some View{
ZStack{
self.backgroundColor
VStack{
ForEach(0...1, id: \.self){ entry in
Text("\(entry)")
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With