2023 SwiftUI & UI FrameworksApp Services
WWDC23 · 7 min · SwiftUI & UI Frameworks / App Services
Bring widgets to new places
The widget ecosystem is expanding: Discover how you can use the latest WidgetKit APIs to make your widget look great everywhere. We’ll show you how to identify your widget’s background, adjust layout dynamically, and prepare colors for vibrant rendering so that your widget can sit seamlessly in any environment.
Watch at developer.apple.com ↗Chapters
Code shown on screen · 4 snippets
SafeAreasWidget
struct SafeAreasWidgetView: View {
(\.widgetContentMargins) var margins
var body: some View {
ZStack {
Color.blue
Group {
Color.lightBlue
Text("Hello, world!")
}
.padding(margins)
}
}
}
struct SafeAreasWidget: Widget {
var body: some WidgetConfiguration {
StaticConfiguration(...) {_ in
SafeAreasWidgetView()
}
.contentMarginsDisabled()
}
} EmojiRangerWidget systemSmall
struct EmojiRangerWidgetEntryView: View {
var entry: Provider.Entry
(\.widgetFamily) var family
var body: some View {
switch family {
case .systemSmall:
ZStack {
AvatarView(entry.hero)
.widgetURL(entry.hero.url)
.foregroundColor(.white)
}
.containerBackground(for: .widget) {
Color.gameBackground
}
}
// additional cases
}
} EmojiRangerWidget accessoryRectangular
var body: some View {
switch family {
case .accessoryRectangular:
HStack(alignment: .center, spacing: 0) {
VStack(alignment: .leading) {
Text(entry.hero.name)
.font(.headline)
.widgetAccentable()
Text("Level \(entry.hero.level)")
Text(entry.hero.fullHealthDate, style: .timer)
}.frame(maxWidth: .infinity, alignment: .leading)
Avatar(hero: entry.hero, includeBackground: false)
}
.containerBackground(for: .widget) {
Color.gameBackground
}
// additional cases
} PhotoWidget
struct PhotoWidget: Widget {
public var body: some WidgetConfiguration {
StaticConfiguration(...) { entry in
PhotoWidgetView(entry: entry)
}
.containerBackgroundRemovable(false)
}
} Related sessions
-
19 min -
18 min