Padding Issue in SwiftUI Widgets

I'm currently working on a SwiftUI widget and have run into an unexpected issue. The image displays as expected (and worked correctly on iOS16), however on iOS17 I have unwanted padding on the left and right of my background image and can't figure out the cause. The padding appears even though I haven't explicitly added any to the design. This behaviour exists even if I have a solid coloured background. It's as though the ZStack doesn't fill the entire widget of the widget space.

Here's a snippet of the code I'm working with:

case .systemMedium:            
            ZStack {
                Image(uiImage: entry.tripImg ?? UIImage())
                    .resizable()
                    .scaledToFill()
                Text("BG image should fill")
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)

I've tried various solutions to remove the padding, but nothing seems to work. I'm not sure if it's a default SwiftUI behavior or if I've missed something in my code.

Has anyone else encountered this issue with SwiftUI widgets? I'd appreciate any insights or advice on how to resolve this.

Thank you in advance!

Answered by darkpaw in 768828022

Without an image of what you're describing, this looks like you need to add .contentMarginsDisabled() on your WidgetConfiguration:

struct WidgetExtension: Widget
{
	var body: some WidgetConfiguration {
		AppIntentConfiguration(
			kind: "kind",
			intent: MyIntent.self,
			provider: TimelineProvider()
		) { entry in
			WidgetEntry(entry: entry)
		}
		.configurationDisplayName("display name")
		.description("description")
		.supportedFamilies([.accessoryCircular, .accessoryInline, .accessoryRectangular, .systemSmall, .systemMedium])
		.contentMarginsDisabled()  // <--- THIS
	}
}
Accepted Answer

Without an image of what you're describing, this looks like you need to add .contentMarginsDisabled() on your WidgetConfiguration:

struct WidgetExtension: Widget
{
	var body: some WidgetConfiguration {
		AppIntentConfiguration(
			kind: "kind",
			intent: MyIntent.self,
			provider: TimelineProvider()
		) { entry in
			WidgetEntry(entry: entry)
		}
		.configurationDisplayName("display name")
		.description("description")
		.supportedFamilies([.accessoryCircular, .accessoryInline, .accessoryRectangular, .systemSmall, .systemMedium])
		.contentMarginsDisabled()  // <--- THIS
	}
}

That was exactly the solve! Thanks so much for your reply, @darkpaw!

Padding Issue in SwiftUI Widgets
 
 
Q