我有以下内容:
如果有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张图像(否则),则该图像将显示出良好的图像,完整的高度以及所有内容。
答案 0 :(得分:1)
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 !!