滚动视图中的 SwiftUI HStack 无法正确显示图像
SwiftUI HStack within a scroll view not showing images correctly
我有以下内容:
它只显示 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: {
[=10=].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: {
[=10=].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: {
[=10=].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: {
[=10=].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: {
[=10=].image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: (UIScreen.main.bounds.width - 33), height: 500)
}
)
// .. other content here
}
.frame(height: 500) // << here !!
我有以下内容:
它只显示 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: {
[=10=].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: {
[=10=].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: {
[=10=].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: {
[=10=].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: {
[=10=].image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: (UIScreen.main.bounds.width - 33), height: 500)
}
)
// .. other content here
}
.frame(height: 500) // << here !!