簡體   English   中英

全屏視圖無法在HStack和水平ScrollView中使用

[英]Full Screen View not working inside HStack and Horizontal ScrollView

我一直在嘗試使用水平列表等創建入職流程。我創建了一個名為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)
}

}}

這就是上面的代碼:

在此處輸入圖片說明

現在,我嘗試使用HStack在ScrollView中添加此視圖。

struct ContentView: View {

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

  }
 }

上面的代碼的結果是荒謬的! 這就是我得到的。 如何解決這個問題? 幫助將不勝感激! 謝謝。

在此處輸入圖片說明

一種可能的解決方案是將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)
    }
}

結果

寬度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM