如何删除内容视图周围的填充并让内容视图填充 Widget iOS 17+ 和 MacOS Sonoma 中的整个区域?

Yod*_*ama 15 ios widgetkit watchos swiftui macos-sonoma

我尝试使用.ignoresSafeArea() ,但没有成功。

iOS 16 和早期版本中的内容视图周围没有填充。

struct CommonDailyEyeTipsWidget: Widget {
    let kind: String = "CommonDailyEyeTipsWidget"

    init() {
        //setup firebase
        FirebaseApp.configure()
    }
    
    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            Rectangle()
                .foregroundStyle(Color.primary)
                .ignoresSafeArea() // Does not work
                .containerBackground(.accent, for: .widget)
            
        }
        .contentMarginsDisabled()
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    }
}

Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Yod*_*ama 21

是的,小部件中的安全区域已被内容边距的使用所取代。这意味着像ignoresSafeArea这样的修饰符在小部件中不再有任何效果。

您仍然可以通过将 contentMarginsDisabled 修饰符添加到小部件配置中来实现相同的效果。

struct CommonDailyEyeTipsWidget: Widget {
    let kind: String = "CommonDailyEyeTipsWidget"

    init() {
        //setup firebase
        FirebaseApp.configure()
    }
    
    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            Rectangle()
                .foregroundStyle(Color.primary)
                .containerBackground(.accent, for: .widget)
        }
        .contentMarginsDisabled() // Here
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:对于任何应保留在默认内容边距内的内容,只需重新添加填充即可。您可以使用 widgetContentMargins 环境变量来获取当前环境的默认边距。

前任:

struct CommonDailyEyeTipsWidgetEntryView : View {
    @Environment(\.widgetContentMargins) var margins
    
    var entry: Provider.Entry
    
    var body: some View {
        Rectangle()
            .foregroundStyle(Color.primary)
            .padding(margins) // If you want a margin 
    }
}
Run Code Online (Sandbox Code Playgroud)

内容边距

内容边距

内容边距是自动应用于小部件主体的填充,可防止您的内容接近小部件容器的边缘。这些边距可能更大或更小,具体取决于显示小部件的环境。


mve*_*ich 11

此外,由于自 iOS 17 以来就出现了边距,因此如果您需要支持较低版本并完全禁用边距,则可以使用此扩展。

extension WidgetConfiguration {
    func disableContentMarginsIfNeeded() -> some WidgetConfiguration {
        if #available(iOSApplicationExtension 17.0, *) {
            return self.contentMarginsDisabled()
        } else {
            return self
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用示例:

struct CustomWidget: Widget {
    private let kind: String = "CustomWidget"
    
    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind,
                            intent: YourCustomIntent.self,
                            provider: YourCustomTimelineProvider()) { entry in
            YourCustomView(entry: entry)
        }
        .configurationDisplayName("Name of widget")
        .description("Description")
        .disableContentMarginsIfNeeded() // add to your widget configuration
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 实际上,无需进行 iOS 17 检查即可使用“.contentMarginsDisabled()”。正如文档所说:_“此修饰符对 iOS 17、watchOS 10 或 macOS 14 之前的操作系统版本没有影响。”_ (2认同)
  • @Kjuly 好点。扩展对于那些需要支持 iOS 15 以下版本的人很有用。 (2认同)