Xcode 15 + iOS 17 adds extra padding to widgets

Xcode 15 and iOS 17 now adds extra padding to widget content, which is inconsistent with iOS 16.

Run on iOS 16:

Same code run on iOS 17, with extra paddings:

struct WidgetView : View {
    var entry: Provider.Entry

    var body: some View {
        LicenseView(entry: entry)
            .background(Color("WidgetBackground"))
            .modifier {
                if #available(iOS 17.0, *) {
                    $0.containerBackground(for: .widget) {
                        Color.white // to highlight the padding on iOS 17
                    }
                } else {
                    $0
                }
            }
    }
}
Answered by Apple dicker in 755438022

If you want to deal with widget margins with full responsibility

extension WidgetConfiguration
{
    func contentMarginsDisabledIfAvailable() -> some WidgetConfiguration
    {
        if #available(iOSApplicationExtension 17.0, *)
        {
            return self.contentMarginsDisabled()
        }
        else
        {
            return self
        }
    }
}
Accepted Answer

If you want to deal with widget margins with full responsibility

extension WidgetConfiguration
{
    func contentMarginsDisabledIfAvailable() -> some WidgetConfiguration
    {
        if #available(iOSApplicationExtension 17.0, *)
        {
            return self.contentMarginsDisabled()
        }
        else
        {
            return self
        }
    }
}

This is probably a newbie type question, but is it possible to fix this in an Xcode 14 build? The extension does work well on a build in Xcode 15, but we're not supposed to submit Xcode 15 builds to the App Store currently, right (but maybe this is a bad assumption)? I have feedback from some users on my app who are already using iOS 17 and reporting the padding issue.

It's better to add conditional compilation.

In this way code will compile and work correct in Xcode 14 and 15.

extension WidgetConfiguration
{
    func contentMarginsDisabledIfAvailable() -> some WidgetConfiguration
    {
        #if compiler(>=5.9) // Xcode 15
            if #available(iOSApplicationExtension 17.0, *) {
                return self.contentMarginsDisabled()
            }
            else {
                return self
            }
        #else
            return self
        #endif
    }
}

Hi! I just want to update this post and let you know the currently selected solution is outdated. On the latest Xcode 15 beta (4), and latest iOS 17 beta (4), using the .contentMarginsDisabled() modifier will compile the widget and automatically do nothing if running on a device below iOS 17. https://developer.apple.com/documentation/swiftui/widgetconfiguration/contentmarginsdisabled() As per Apple documentation, this modifier now has no effect on prior operating systems.

Xcode 15 + iOS 17 adds extra padding to widgets
 
 
Q