我一直在尝试使用水平列表等创建入门流程。我创建了一个名为 OnboardingView
的 View ,其中有一个带有图像的 VStack 和两个 TextView 。
struct OnboardingView: View {
var body: some View {
GeometryReader { geometry in
VStack(spacing: 10) {
Spacer()
Image("1")
.resizable()
.frame(width: 300, height: 300)
.clipShape(Circle())
.padding(20)
Text("Travel the World")
.frame(width: geometry.size.width, height: 20, alignment: .center)
.font(.title)
Text("Explore the world and get to know different cultures and people from all around the world")
.lineLimit(nil)
.padding(.leading, 15)
.padding(.trailing, 15)
.font(.system(size: 16))
.frame(width: geometry.size.width, height: 50, alignment: .center)
.multilineTextAlignment(.center)
Spacer()
}.background(Color.red)
}
} }
这是我通过上面的代码得到的结果:
现在我尝试使用 HStack 将此 View 添加到 ScrollView 中。
struct ContentView: View {
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
OnboardingView()
OnboardingView()
}
}.background(Color.black)
}
}
上面代码的结果是荒谬的!这就是我得到的。如何解决这个问题?帮助将不胜感激!谢谢。
最佳答案
一种可能的解决方案是将 OnboardingView
的 width
设置为 geometry.size.width
:
var body: some View {
GeometryReader { geometry in
ScrollView(.horizontal, showsIndicators: false) {
HStack {
OnboardingView()
.frame(width: geometry.size.width)
OnboardingView()
.frame(width: geometry.size.width)
}
}.background(Color.black)
}
}
Result
关于ios - 全屏 View 在 HStack 和水平 ScrollView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57315159/