实现Dynamic Island支持遇报错:Generic parameter 'Expanded'无法推断
问题原因与解决方案
核心原因
你的项目最低部署目标是iOS 16.1,但Dynamic Island 对 Live Activities 的支持是从 iOS 16.2 才正式引入的。iOS 16.1 的 WidgetKit API 中并没有 ActivityConfiguration 的 dynamicIsland 参数,也不存在 DynamicIslandExpandedContentBuilder 相关的结果构建器实现,这就是编译报错的根源。
两种解决方式
方式一:升级最低部署目标到 iOS 16.2
直接将 Widget 扩展的最低部署版本调整为 iOS 16.2(主应用可保留 iOS 16.1,Widget 扩展需单独设置):
- 选中项目中的 Widget 扩展目标
- 在「General」标签的「Deployment Info」里,将「iOS Deployment Target」改为 16.2
- 重新编译后原代码即可正常运行
方式二:兼容 iOS 16.1 与 iOS 16.2+
如果必须保留 iOS 16.1 作为最低版本,需要通过版本条件编译分别提供适配:
import SwiftUI import WidgetKit @main struct PizzaDeliveryActivityWidget: Widget { var body: some WidgetConfiguration { if #available(iOS 16.2, *) { ActivityConfiguration(for: PizzaDeliveryAttributes.self) { context in // 锁屏及非 Dynamic Island 设备的横幅内容 Text("Delivery in progress") } dynamicIsland: { context in DynamicIsland { // 展开状态的 Dynamic Island 内容 EmptyView() } compactLeading: { EmptyView() } compactTrailing: { EmptyView() } minimal: { EmptyView() } } } else { // iOS 16.1 仅支持锁屏横幅,无 Dynamic Island ActivityConfiguration(for: PizzaDeliveryAttributes.self) { context in Text("Delivery in progress") } } } }
注意:iOS 16.1 的设备无法显示 Dynamic Island 内容,只会展示锁屏横幅,这是系统限制。
内容的提问来源于stack exchange,提问作者Curiosity
相关产品推荐
相关产品推荐

