Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update widget for iOS 17 #948

Open
onmyway133 opened this issue Oct 2, 2023 · 0 comments
Open

How to update widget for iOS 17 #948

onmyway133 opened this issue Oct 2, 2023 · 0 comments

Comments

@onmyway133
Copy link
Owner

onmyway133 commented Oct 2, 2023

iOS 17 has a new Stand by mode so SwiftUI introduces containerBackground for the system to decide when to draw background. It also automatically applies margin to widget so we may need to disable that

To update existing widgets, we can write some useful extension

extension View {
    @ViewBuilder
    func safeContainerBackground(@ViewBuilder content: () -> some View) -> some View {
        if #available(iOS 17.0, *) {
            self.containerBackground(for: .widget, content: content)
        } else {
            self.background(content())
        }
    }
}

extension WidgetConfiguration {
    func safeContentMarginsDisabled() -> some WidgetConfiguration {
        if #available(iOS 15.0, *) {
            return contentMarginsDisabled()
        } else {
            return self
        }
    }
}

So in our Widget configuration, we can use

struct BalanceWidget: Widget {
    var body: some WidgetConfiguration {
        IntentConfiguration(
            kind: kind,
            intent: SelectAccountIntent.self,
            provider: BalanceTimelineProvider()
        ) { entry in
            BalanceWidgetView(entry: entry)
        }
		.safeContentMarginsDisabled()
    }
}

struct BalanceWidgetView: View {
    var body: some View {
        ZStack {
            ...
        }
        .safeContainerBackground {
            Color.green
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant