swiftuiwidgetkit

Make Image Fill Entire Widget


I'm trying to create a widget for my iOS app. I have an Image I'm trying to set as a background image, which works, except it doesn't fill the entire widget area.

Here's my code:

struct homeWidgetEntryView : View {
    var entry: Provider.Entry
    var ImageWidget: some View {
        if let uiImage = UIImage(contentsOfFile: entry.fileName){
            let image = Image(uiImage: uiImage)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .edgesIgnoringSafeArea(.top)
            
            return AnyView(image)
        }
        
        print("The image file could not be loaded")
        return AnyView(EmptyView())
    }
    
    var body: some View {
        ImageWidget
    }

And here's the result:

enter image description here


Solution

  • Try replacing aspectRatio(contentMode: .fit) with scaledToFill() or scaledToFit().