Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

ios - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...