Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some views appear as a red no entry sign in widgets?

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:

The No Entry sign shown in the xcode 12 preview

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

like image 216
Paul Avatar asked Jul 28 '20 13:07

Paul


3 Answers

Background

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.


List of views that are not allowed in WidgetKit

Note that the list is not exhaustive, provided mostly with examples:

  1. Interactive views (ones you can interact with, e.g., by touching them):

    • List
    • Form
    • ScrollView
    • TabView
    • Map
    • Picker
    • Slider
    • and so on...
  2. Animated views:

    • ProgressView
  3. Navigation views (you can't use the standard navigation - for this you have Link):

    • NavigationView
    • NavigationLink
  4. UIKit Wrappers for UIKit Views/ViewControllers:

    • UIViewRepresentable
    • UIViewControllerRepresentable

Replacements

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.

like image 142
pawello2222 Avatar answered Sep 20 '22 14:09

pawello2222


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.

like image 25
Jackson Avatar answered Sep 16 '22 14:09

Jackson


Use ForEach inside a VStack:

 var body: some View{
        ZStack{
            self.backgroundColor
            VStack{
                ForEach(0...1, id: \.self){ entry in
                    Text("\(entry)")
                }
            }
        }
    }
like image 20
the Reverend Avatar answered Sep 19 '22 14:09

the Reverend