我使用以下代码创建一个启动控制小部件:
@available(iOS 18.0, *)
struct LockScreenAppLaunchWidget: ControlWidget {
var body: some ControlWidgetConfiguration {
StaticControlConfiguration(kind: "com.example.lockscreenlauncher") {
ControlWidgetButton(action: LaunchAppIntent()) {
Label {
Text("AppName")
} icon: {
Image("custom_icon").renderingMode(.original).resizable()
}
}
}
.displayName("Open app")
}
}
我创建了一个新的 xcassets 文件来存储此图标图像,名为 Media.xcassets,其目标附加到 WidgetExtension 和 main APP。
但是当我运行构建时,该控件的图标仍然显示为占位符(破折号矩形中的?符号)。
以下是一些尝试
Label("AppName", systemImage: "heart") // is ok
Label("AppName", image: "custom_icon") // is not ok
根据 Apple 文档 ControlWidget 符合 ControlWidgetTemplate 其中规定
这些模板定义图像(特别是符号图像)
因此,为了拥有自定义图像,该图像必须是 SF 符号。您可以参考这个文档进行自定义实现。或者您可以简单地使用 App Store 中的创建自定义符号应用程序。只需在应用程序中打开现有的 SVG,它就会创建一个自定义的 SFSymbol。