【问题标题】:SwiftUI: How to get scrollview to contain full list lengthSwiftUI:如何让滚动视图包含完整列表长度
【发布时间】:2020-01-22 01:00:35
【问题描述】:

我有一个滚动视图,其中包含一个 vstack,其中包含一些文本和一个列表。列表独立于滚动视图滚动,导致文本成为列表标题的效果。我想让滚动视图改为完全展开列表,以便所有内容一起滚动。

ScrollView {
                VStack(alignment: .leading) {
                    Text(self.person.name)

                    Text(self.person.email)
                        .font(.subheadline)
                        .padding(.top)

                    Text(self.person.about)
                        .foregroundColor(.secondary)
                        .padding()

                    List {
                        Section {
                            ForEach(self.friends, id: \.id) { friend in
                                Text(friend.name)
                            }
                        }
                    }
                    Spacer()
                 }
                .padding()

            }

这是我滚动列表时得到的:

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    您在这里有效地获得了两个 ScrollView。您需要使用单个 List 并将其他项目放置为 Section 标头。不幸的是,List 似乎并不像 UITableView 那样支持整个视图的标题。

        var body: some View {
            List {
                Section(header:
                    VStack(alignment: .leading) {
                        Text(self.person.name)
    
                        Text(self.person.email)
                            .font(.subheadline)
                            .padding(.top)
    
                        Text(self.person.about)
                            .foregroundColor(.secondary)
                            .padding()
                    }
                ) {
                    ForEach(self.friends, id: \.id) { friend in
                        Text(friend.name)
                    }
                }
            }
        }
    

    编辑:也许您甚至没有在寻找标题?在这种情况下,只需将您想要的内容放在ForEach 上方即可。

        var body: some View {
            List() {
                Section {
    
                    VStack(alignment: .leading) {
                        Text(self.person.name)
    
                        Text(self.person.email)
                            .font(.subheadline)
                            .padding(.top)
    
                        Text(self.person.about)
                            .foregroundColor(.secondary)
                            .padding()
                    }
    
                    ForEach(self.friends, id: \.id) { friend in
                        Text(friend.name)
                    }
                }
            }
        }
    

    这是有标题和没有标题的结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多