onmyway133 / blog

🍁 What you don't know is what you haven't learned
https://onmyway133.com/
MIT License
679 stars 33 forks source link

How to scale image fill without affect layout in SwiftUI #939

Open onmyway133 opened 1 year ago

onmyway133 commented 1 year ago

Instead of letting the Image decide the size, we can put it as background or overlay. I use clipped and contentShape to avoid the fill image obscuring touch event

Color.clear
    .frame(height: 100)
    .overlay {
        AsyncImage(url: item.imageUrl) { image in
            image
                .resizable()
                .scaledToFill()
        } placeholder: {
            ProgressView()
        }
    }
    .clipped()
    .contentShape(Rectangle())