簡體   English   中英

ScrollView內容未填寫SwiftUI

[英]ScrollView content not filling in SwiftUI

我有一個scrollview,其內容是一個包含ForEach循環的VStack ,用於創建一些行而不是列表。 列表有一些缺點,如分隔線。

我的問題是Row沒有填充scrollview。 我認為滾動視圖寬度沒有填滿屏幕。

NavigationView {
        Toggle(isOn: $onlineStatus) {
            Text("Online Only")
        }.padding([.leading, .trailing], 15)

        ScrollView {
            VStack(alignment: .trailing) {
                ForEach(onlineStatus ? self.notes.filter { $0.dot == .green } : self.notes) { note in
                    NavigationButton(destination: Text("LOL")) {
                        CardRow(note: note)
                            .foregroundColor(.primary)
                            .cornerRadius(8)
                            .shadow(color: .gray, radius: 3, x: 0, y: -0.01)
                    }.padding([.leading, .trailing, .top], 5)
                }.animation(self.onlineStatus ? .fluidSpring() : nil)
            }
        }.padding([.leading, .trailing])

        .navigationBarTitle(Text("Your documents"))
    }

這給了我這個結果:

在此輸入圖像描述

那是我的CardRow:

struct CardRow: View {
var note: Note

var body: some View {
    HStack {
        Image(uiImage: UIImage(named: "writing.png")!)
            .padding(.leading, 10)

        VStack(alignment: .leading) {
            Group {
                Text(note.message)
                    .font(.headline)
                Text(note.date)
                    .font(.subheadline)
            }
            .foregroundColor(.black)
        }

        Spacer()
        VStack(alignment: .trailing) {
            Circle().foregroundColor(note.dot)
                .frame(width: 7, height: 7)
                .shadow(radius: 1)
                .padding(.trailing, 5)
            Spacer()
        }.padding(.top, 5)
    }
    .frame(height: 60)
    .background(Color(red: 237/255, green: 239/255, blue: 241/255))
}

}

在RowView或其內部使用.frame(minWidth: 0, maxWidth: .infinity)

嘗試將RowView的幀寬設置為UIScreen.main.bounds.width: .frame(width: UIScreen.main.bounds.width)

我找到的最佳解決方案是使用GeometryReader將ScrollView的內容填充到外部視圖的寬度。 並確保在每一行的HStack中使用Spacer()。 這可以很好地處理安全區域和旋轉。

struct Card : View {
    var body: some View {
        HStack {
            VStack(alignment: .leading) {
                Text("Header")
                    .font(.headline)
                Text("Description")
                    .font(.body)
            }
            Spacer()
        }
            .padding()
            .background(Color.white)
            .cornerRadius(8)
            .shadow(color: Color.black.opacity(0.15), radius: 8, x: 0, y: 0)
    }
}

struct Home : View {
    var body: some View {
        GeometryReader { geometry in
            NavigationView {
                ScrollView {
                    VStack(alignment: .leading, spacing: 16) {
                        Card()
                        Card()
                        Card()
                    }
                        .padding()
                        .frame(width: geometry.size.width)
                }
                    .navigationBarTitle(Text("Home"))
            }
        }
    }
}

在此處查看結果截圖。

暫無
暫無

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

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