From 5e2106f3ba04e023040e78dd113c7925c544ef08 Mon Sep 17 00:00:00 2001 From: Rainvisitor Date: Sun, 7 Jan 2024 01:15:31 +0800 Subject: [PATCH] feat: update iOS home widget for iOS 17 API --- ios/CourseAppWidget/CourseAppWidget.swift | 25 ++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ios/CourseAppWidget/CourseAppWidget.swift b/ios/CourseAppWidget/CourseAppWidget.swift index f0fe2565..34ab4ae6 100644 --- a/ios/CourseAppWidget/CourseAppWidget.swift +++ b/ios/CourseAppWidget/CourseAppWidget.swift @@ -119,7 +119,7 @@ struct CourseAppWidgetEntryView: View { .padding([.trailing, .leading], 8) .multilineTextAlignment(.center) } - .background(getContentBackgroudColor()) + .widgetBackground(getContentBackgroudColor()) } } } @@ -134,6 +134,7 @@ struct CourseAppWidget: Widget { } .configurationDisplayName("上課提醒") .description("提醒本日下一堂課") + .disableContentMarginsIfNeeded() } } @@ -143,3 +144,25 @@ struct CourseAppWidget_Previews: PreviewProvider { .previewContext(WidgetPreviewContext(family: .systemSmall)) } } + +extension View { + func widgetBackground(_ backgroundView: some View) -> some View { + if #available(iOSApplicationExtension 17.0, *) { + return containerBackground(for: .widget) { + backgroundView + } + } else { + return background(backgroundView) + } + } +} + +extension WidgetConfiguration { + func disableContentMarginsIfNeeded() -> some WidgetConfiguration { + if #available(iOSApplicationExtension 17.0, *) { + return self.contentMarginsDisabled() + } else { + return self + } + } +}