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
                }
            }
    }
}

Accepted Reply

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
        }
    }
}
  • Thanks for this! Are you able to use this in preview? It is working for me in the app builds but previews are stuck with the padding due to using the internal views, not the Widget child class itself

  • Also check out the “Bring widgets to new places" WWDC23 video for tips on applying appropriate padding for the different places widgets can appear.

  • 👍 Very helpful. Thank you!

Replies

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
        }
    }
}
  • Thanks for this! Are you able to use this in preview? It is working for me in the app builds but previews are stuck with the padding due to using the internal views, not the Widget child class itself

  • Also check out the “Bring widgets to new places" WWDC23 video for tips on applying appropriate padding for the different places widgets can appear.

  • 👍 Very helpful. Thank you!

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.

  • @aviwad Thanks for the update! Confirmed contentMarginsDisabled() is back deployed to iOS 15/macOS 12/watchOS 9.0.

Add a Comment