为什么有些视图在小部件中显示为红色的禁止进入标志? [英] Why do some views appear as a red no entry sign in widgets?

查看:18
本文介绍了为什么有些视图在小部件中显示为红色的禁止进入标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 xcode 12 中创建了一个项目并添加了一个小部件扩展目标.目标已创建,我只剩下一个显示时间的简单小部件.小部件的预览按预期工作,小部件在模拟器中按预期显示.但是,如果我将视图更改为使用 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 时也遇到了这个问题.该视图是图表库中的 LineChartView.~ 根据 Apple 开发者论坛上的这篇帖子 无法在 WidgetKit 小部件中使用 UIKit 视图.

~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:

  1. 交互式视图(您可以与之交互的视图,例如,通过触摸它们):

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

  • 列表
  • 表格
  • ScrollView
  • TabView
  • 地图
  • 选择器
  • 滑块
  • 等等...

动画视图:

  • ProgressView

导航视图(你不能使用标准导航——为此你有Link):

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

  • NavigationView
  • NavigationLink

UIKit 包装器,用于 UIKit 视图/视图控制器:

UIKit Wrappers for UIKit Views/ViewControllers:

  • UIViewRepresentable
  • UIViewControllerRepresentable


更换

如果您想使用上述任何视图,例如一个像List这样的交互式视图,你需要从非交互式视图中创建它——这里是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆