簡體   English   中英

SwiftUI iOS14 Widget - 使用相同的 WidgetFamily 創建多布局

[英]SwiftUI iOS14 Widget - Create create multi layout with the same WidgetFamily

當我從主屏幕添加小部件時,我可以看到 Apple 創建的時鍾應用程序有兩個小的WidgetFamily布局。

如何在時鍾應用程序中制作兩個小部件?

我看到我只能為每個WidgetFamily創建一個布局。

似乎您需要創建具有自己的視圖、條目、提供程序的單獨小部件...

這是使用WidgetBundle的可能解決方案:

  1. 創建單獨的小部件(確保@main注釋附加到其中任何一個):
struct Widget1: Widget {
    let kind: String = "Widget1"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Widget1Provider()) { entry in
            Widget1Entry(entry: entry)
        }
        .configurationDisplayName("Widget1")
        .description("This is an example widget v1.")
    }
}

struct Widget2: Widget {
    let kind: String = "Widget2"

    var body: some WidgetConfiguration {
        ...
    }
}

...
  1. 為每個小部件創建自己的視圖(也可能是單獨的條目和提供者,具體取決於您的需要):
struct Widget1EntryView: View {
    var entry: Widget1Entry

    var body: some View {
        Text("Widget1")
    }
}

struct Widget2EntryView: View {
    var entry: Widget2Entry

    var body: some View {
        Text("Widget2")
    }
}

...
  1. 使用WidgetBundle提供一個包含你的小部件的
@main
struct WidgetsBudle: WidgetBundle {
    var body: some Widget {
        Widget1()
        Widget2()
        // add more Widgets if you want
    }
}

請注意, @main附加到WidgetsBudle而不是 Widgets。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM