swiftuiswiftui-asyncimage

Image doesn't appear with AsyncImage in SwiftUI


I have AsyncImage inside a TabView. When I do this the image never appears. I just see the progress bar.

@available(iOS 15.0, *)
struct TEST: View {
    var body: some View {
        VStack {
            TabView {
                AsyncImage(url: URL(string: "https://blckbirds.com/wp-content/uploads/2021/10/pexels-kammeran-gonzalezkeola-6128227-2.jpg"), scale: 2) { image in
                    image
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                } placeholder: {
                    ProgressView()
                        .progressViewStyle(.circular)
                }

            }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
        }
    }
}

Solution

  • try using a ZStack to wrap the AsyncImage, like this, works for me:

    struct TEST: View {
        
        var body: some View {
            VStack {
                TabView {
                    ZStack {   // <--- here
                        AsyncImage(url: URL(string: "https://blckbirds.com/wp-content/uploads/2021/10/pexels-kammeran-gonzalezkeola-6128227-2.jpg"), scale: 2) { image in
                            image
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                        } placeholder: { ProgressView().progressViewStyle(.circular) }
                    }
                }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
            }
        }
        
    }