为什么某些视图在小部件中显示为红色的“禁止进入"符号? [英] Why do some views appear as a red no entry sign in widgets?
问题描述
我已经用xcode 12创建了一个项目,并添加了一个Widget Extension目标.目标已创建,剩下的是一个显示时间的简单小部件.小部件的预览按预期方式工作,并且小部件在模拟器中按预期显示.但是,如果我更改视图以使用 List
而不是 Text
,如下所示:
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:
我尝试将 List
放入 VStack
内,并使用动态列表,但没有成功.我找不到在小部件中被禁止的任何有关 List
的文档.我尝试重新启动xcode和模拟器,进行清理和重建.我正在使用xcode 12.0 beta 3.
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.
〜我也遇到这个问题,尝试使用包装在 UIViewRepresentable
中的 UIView
.该视图是Charts库中的 LineChartView
.〜根据
~I also get this issue trying to use a UIView
wrapped in UIViewRepresentable
. The view is a LineChartView
from the Charts library.~ according to this post on the Apple developer forums it isn't possible to use UIKit views in a WidgetKit widget.
有人知道我如何在小部件中使用 List
吗?任何帮助,不胜感激.谢谢
Does anyone know how I can use a List
in a widget? Any help much appreciated. Thanks
推荐答案
背景
小部件视图为 静态 .
他们不支持动画,导航或用户交互( Link
除外).而且必须使用纯SwiftUI编写.
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):
-
列表
-
表格
-
ScrollView
-
TabView
-
地图
-
Picker
-
滑块
- 依此类推...
动画视图:
-
ProgressView
导航视图(您不能使用标准导航-为此,您具有 Link
):
Navigation views (you can't use the standard navigation - for this you have Link
):
-
NavigationView
-
NavigationLink
用于UIKit视图/视图控制器的UIKit包装器:
-
UIViewRepresentable
-
UIViewControllerRepresentable
替换
如果要使用以上任何视图,例如一个像 List
这样的 interactive 视图,您需要在 non-interactive 视图之外创建它-这里是 VStack
+ ForEach
.
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
.
这篇关于为什么某些视图在小部件中显示为红色的“禁止进入"符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!