我想为每个小部件尺寸(即小、中、大)创建不同的布局。如何根据小部件的大小分支我的代码?
问问题
5715 次
2 回答
42
作为其中一部分的WidgetFamily
( Apple Documentation ) 枚举WidgetKit
将允许您在视图中切换各种尺寸并进行相应调整。将此设置为@Environment
变量并打开可用案例:
.systemSmall
.systemMedium
.systemLarge
struct WidgetView : View {
@Environment(\.widgetFamily) var family
@ViewBuilder
var body: some View {
switch family {
case .systemSmall:
Text("Small")
case .systemMedium:
Text("Medium")
case .systemLarge:
Text("Large")
default:
Text("Some other WidgetFamily in the future.")
}
}
}
于 2020-08-04T12:16:37.323 回答
4
除了接受的答案之外,在您的 Provider 类方法(getTimeline、getSnapshot 和 placeholder)中,您会获得一个具有家庭成员 var的上下文对象。
family可以是以下三种小部件尺寸之一:.systemSmall、.systemMedium 和 .systemLarge
苹果官方文档。
于 2020-10-02T14:13:50.313 回答