全屏视图无法在HStack和水平ScrollView中使用

时间:2019-08-01 18:38:22

标签: ios scrollview swiftui horizontallist hstack

我一直在尝试使用水平列表等创建入职流程。我创建了一个名为OnboardingView的视图,在其中有一个带有图像的VStack和两个Text视图。

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)
}

}   }

这是我从上面的代码中得到的:

enter image description here

现在,我正在尝试使用HStack在ScrollView中添加此视图。

struct ContentView: View {

var body: some View {
   ScrollView(.horizontal, showsIndicators: false) {
        HStack {
            OnboardingView()
            OnboardingView()
        }
    }.background(Color.black)

  }
 }

以上代码的结果是荒谬的!这就是我得到的。如何解决这个问题?帮助将不胜感激!谢谢。

enter image description here

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是将width中的OnboardingView设置为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)
    }
}
  

结果

width