外观更改时更新小部件

时间:2020-10-31 15:08:55

标签: swift swiftui ios14 widgetkit

当外观从$password = 'abcABC123'; echo preg_match("/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)[a-zA-Z\d]{1,11}$/", $password); // Output: 1 $password_array = [ 'abcdef', 'abcDEF', 'abcdefghiJKL123', '111111', 'AAAAAA', 'ABC123', '1_magical', '"special2A', '%abcDEF123', 'ABCabc123', 'somePass1', 'Password99', 'alphaNum1' ]; foreach($password_array as $key=>$password){ echo "\n{$key} => "; echo preg_match("/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)[a-zA-Z\d]{1,11}$/", $password); } /* Output: 0 => 0 1 => 0 2 => 0 3 => 0 4 => 0 5 => 0 6 => 0 7 => 0 8 => 0 9 => 1 10 => 1 11 => 1 12 => 1 */ 模式更改为light模式,反之亦然时,我需要知道是否有人知道SwiftUI中的系统来更新Widget。

我可以更改文本和图像,但是我使用一种方法来显示地图的屏幕截图,每次外观更改时都应运行它以获取正确的地图颜色。

1 个答案:

答案 0 :(得分:2)

  1. 创建两个屏幕截图(每个主题一个),并将其传递给条目:
struct SimpleEntry: TimelineEntry {
    let date: Date
    let mapScreenshots: [ColorScheme: Image]
}
struct Provider: TimelineProvider {
    ...

    func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> Void) {
        let entry = SimpleEntry(
            date: Date(),
            mapScreenshots: [
                // replace with actual map screenshots
                .dark: Image(systemName: "plus"),
                .light: Image(systemName: "minus"),
            ]
        )
        let timeline = Timeline(entries: [entry], policy: .never)
        completion(timeline)
    }
}
  1. 使用@Environment(\.colorScheme)对主题更改做出反应:
struct WidgetEntryView: View {
    @Environment(\.colorScheme) var colorScheme

    var entry: Provider.Entry

    var body: some View {
        if let screenshot = entry.mapScreenshots[colorScheme] {
            screenshot
        }
    }
}