swift - 滚动视图中的 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 张图像(其他)时,图像会显示良好、全高和所有内容。
解决方案
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 !!
推荐阅读
- php - 有没有办法用scandir循环文件夹?循环时遇到问题
- scala - 传播两个 Seqs scala
- webpack - Webpack 4如何获得正确的字体路径
- php - 如何通过 PHP 删除空的 CSS 属性?
- android - 片段内的 activity.recreate() 导致底部导航栏停止工作
- xml - 获取导致 IIB 中的转换错误的 XML 字段值的 XPath
- emacs - 有没有办法制作
- javascript - 使用查询字符串 Javascript 过滤对象项
- mongodb - 如何根据具有来自其他集合的值的条件从集合中删除许多文档
- oracle - SQL*Plus 不运行 PL/SQL 块