首页 > 解决方案 > 滚动视图中的 SwiftUI HStack 未正确显示图像

问题描述

我有以下内容:

如果有 1 个图像可用或存在多个图像,它只显示 1 个图像,它会创建一个HStack应该scrollable显示所有图像的图像:

 ScrollView {
        VStack (alignment: .leading) {
            if userData.profileURL1 != nil {
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack {
                        URLImage(URL(string: userData.profileURL)!, placeholder: Image(systemName: "circle"),
                         content:  {
                            $0.image
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: (UIScreen.main.bounds.width - 33),height: 500)
                            }
                        )
                        .cornerRadius(12)
                        .padding([.leading,.trailing])
                        URLImage(URL(string: userData.profileURL1!)!, placeholder: Image(systemName: "circle"),
                         content:  {
                            $0.image
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: (UIScreen.main.bounds.width - 33),height: 500)
                            }
                        )
                        .cornerRadius(12)
                        .padding([.leading,.trailing])
                        if userData.profileURL2 != nil {
                            URLImage(URL(string: userData.profileURL2!)!, placeholder: Image(systemName: "circle"),
                             content:  {
                                $0.image
                                    .resizable()
                                    .aspectRatio(contentMode: .fill)
                                    .frame(width: (UIScreen.main.bounds.width - 33),height: 500)
                                }
                            )
                            .cornerRadius(12)
                            .padding([.leading,.trailing])
                        }
                    }
                }
            } else {
                URLImage(URL(string: userData.profileURL)!, placeholder: Image(systemName: "circle"),
                 content:  {
                    $0.image
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: (UIScreen.main.bounds.width - 33),height: 500)
                    }
                )
                .cornerRadius(12)
                .padding([.leading,.trailing])
            }
       }
}

逻辑有效,如果存在多个图像,它会显示多个图像,但不能正确显示它们,图像的高度没有被应用,据我所知:

在此处输入图像描述

在此处输入图像描述

图像正在滚动以显示另一个,但高度都搞砸了。但是,当仅存在 1 张图像(其他)时,图像会显示良好、全高和所有内容。

标签: swiftswiftuixcode12

解决方案


ScrollView 不知道内容的高度,您必须明确指定它。根据您的代码,它看起来应该是

ScrollView(.horizontal, showsIndicators: false) {
    HStack {
        URLImage(URL(string: userData.profileURL)!, placeholder: Image(systemName: "circle"),
         content:  {
            $0.image
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: (UIScreen.main.bounds.width - 33), height: 500)
            }
        )

      // .. other content here

    }
    .frame(height: 500)     // << here !!

推荐阅读