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

你说的曾经没有我的故事 提交于 2020-12-04 15:58:04

问题


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


回答1:


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.




回答2:


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.




回答3:


Use ForEach inside a VStack:

 var body: some View{
        ZStack{
            self.backgroundColor
            VStack{
                ForEach(0...1, id: \.self){ entry in
                    Text("\(entry)")
                }
            }
        }
    }


来源:https://stackoverflow.com/questions/63135404/why-do-some-views-appear-as-a-red-no-entry-sign-in-widgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!