ios - 全屏 View 在 HStack 和水平 ScrollView 中不起作用

标签 ios scrollview swiftui horizontallist hstack

我一直在尝试使用水平列表等创建入门流程。我创建了一个名为 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)
}

} }

这是我通过上面的代码得到的结果:

enter image description here

现在我尝试使用 HStack 将此 View 添加到 ScrollView 中。

struct ContentView: View {

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

  }
 }

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

enter image description here

最佳答案

一种可能的解决方案是将 OnboardingViewwidth 设置为 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

width

关于ios - 全屏 View 在 HStack 和水平 ScrollView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57315159/

相关文章:

java - 使RelativeLayout可滚动

ios - 重复相同的查询快照,引入相同的数据,尽管新数据被添加到 firestore

ios - Swift xcode 错误 : load controller is not allowed and autoresize not equal to views window

ios - UIFont fontWithSize:更改fontName

ios - 在 Firebase iOS SDK 中,每次删除子项时都会触发 .childAdded。我怎样才能阻止这个?

android - 屏幕滚动后执行操作

android - ScrollView 内的 LinearLayout 自动滚动到底部

core-data - 使用核心数据导航到 SwiftUI 中的详细 View

ios - swift 错误 : Type of expression is ambiguous without more context

swiftui - 如何设置 UIImage 的高度始终与宽度相同?