我想AccessoryWidgetBackground()
向accessoryRectangular
小部件系列添加完整尺寸。
我创建了一个全新的项目并添加了一个全新的小部件。然后我将视图更改为:
struct Some_WidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
AccessoryWidgetBackground()
.frame(width: .infinity, height: .infinity)
.ignoresSafeArea()
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在背景模糊的情况下占据整个视图?
所提出问题的答案是:这在 iOS 16.0 中是不可能的,从问题图像的内框可以看到视图被裁剪。
我选择的解决方法是创建一个圆形背景视图并将其添加到 ZStack 中:
var body: some View {
ZStack {
OptionalBlurView(showBlur: true)
// etc
Run Code Online (Sandbox Code Playgroud)
这是背景视图,它也适用于预览。
@available(iOSApplicationExtension 16.0, *)
struct OptionalBlurView: View {
var showBlur: Bool
@Environment(\.widgetFamily) var family
var body: some View {
if showBlur {
blurView
} else {
EmptyView()
}
}
var blurView: some View {
#if targetEnvironment(simulator)
// at the time of coding, AccessoryWidgetBackground does not show in previews, so this is an aproximation
switch family {
case .accessoryCircular:
return Circle()
.opacity(0.3)
.eraseToAnyView()
default:
return Rectangle()
.clipShape(RoundedRectangle(cornerSize: CGSize(width: 10, height: 10), style: .continuous))
.opacity(0.3)
.eraseToAnyView()
}
#else
AccessoryWidgetBackground()
.clipShape(RoundedRectangle(cornerSize: CGSize(width: 10, height: 10), style: .continuous))
.opacity(0.7)
#endif
}
}
Run Code Online (Sandbox Code Playgroud)