簡體   English   中英

可快速滾動全屏視圖

[英]Scrollable full screen view swift

我希望在 Swift UI 中創建一個全屏可滾動視圖。 我在將視圖放入滾動視圖和垂直堆棧后讓視圖全屏顯示時遇到問題? 有什么建議?

這是我的代碼的樣子

struct ContentView : View {
    var body: some View {

        //var posts = [Post(postColor: .red), Post(postColor: .blue), Post(postColor: .green)]
        ScrollView(.vertical, showsIndicators: false) {

            VStack {
                Post(postColor: .red)
                Post(postColor: .blue)
                Post(postColor: .green)

            }
        }
    }

}

struct Post : View {

    var postColor : Color

    var body : some View {

        VStack(alignment: .leading) {

                HStack {
                    Text("Name")
                        .font(.title)
                        .fontWeight(.heavy)

                    Spacer()
                   }

                Text("@Username")
                    .lineLimit(nil)
                    .font(.body)


                   Spacer()
                   }.background(postColor)

    }

}

圖像這是我嵌入滾動視圖后的輸出。

圖像我希望有一個像這樣的全屏視圖,但能夠向下滾動到下一個視圖(基本上向下滾動到下一個屏幕)

您可以使用GeometryReader來實現這一點:

struct FullScreenViewsInScrollView: View {
    var body: some View {

        GeometryReader { geometry in

            ScrollView {

                VStack(spacing: 0) {
                    Rectangle()
                        .foregroundColor(.red)
                        .frame(height: geometry.size.height)

                    Rectangle()
                        .foregroundColor(.blue)
                        .frame(height: geometry.size.height)

                    Rectangle()
                        .foregroundColor(.green)
                        .frame(height: geometry.size.height)
                }

            }

        }
    }

}

結果應該是(我在實時預覽中向下滾動了一點):

在此處輸入圖片說明

暫無
暫無

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

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