简体   繁体   English

SwiftUI 列表子视图未显示

[英]SwiftUI List subview not showing up

I'm trying to embed a view that contains a Header view and a List The header and list render in the original view, but the list completely disappears when I put it in the parent view.我正在尝试在原始视图中嵌入一个包含 Header 视图和列表 header 和列表渲染的视图,但是当我将它放在父视图中时,该列表完全消失了。 I don't understand what I'm doing wrong.我不明白我做错了什么。 I tried putting the header in the list, but then the entire view disappears.我尝试将 header 放入列表中,但随后整个视图消失了。

Here is the code for the view that contains the list:这是包含列表的视图的代码:

import SwiftUI

struct CityDetailCategoryView: View {
    let cityDetailViewModel: CityDetailViewModel
    let titles = ["Category 2", "Category 3", "Category 4", "Category 5", "Category 6", "Category 7"]
    
    var body: some View {
        VStack(alignment: .leading) {
            SectionHeadingView(cityDetailViewModel: CityDetailViewModel, sectionTitle: "Categories")
            List {
                ForEach(titles, id: \.self) { title in
                    Text(title)
                        .font(.title3)
                        .fontWeight(.medium)
                }
            }
        }
    }
}

struct CityDetailCategoryView_Previews: PreviewProvider {
    static var previews: some View {
        CityDetailCategoryView(cityDetailViewModel: CityDetailViewModel())
            .previewLayout(.sizeThatFits)
    }
}

...and the code of the parent view: ...以及父视图的代码:

import SwiftUI

struct CityDetailView: View {
    @ObservedObject var cityDetailViewModel: CityDetailViewModel
    
    var body: some View {
        NavigationView {
            ScrollView(.vertical, showsIndicators: false, content: {
                VStack(alignment: .leading, spacing: 6, content: {
                    DetailHeaderImageView(cityDetailViewModel: CityDetailViewModel)
                    Group {
                        CityDetailTitleView(cityDetailViewModel: CityDetailViewModel)
                        CityDetailQuickFactsView(cityDetailViewModel: CityDetailViewModel)
                        CityDetailCategoryView(cityDetailViewModel: CityDetailViewModel)
                        CityDetailMiscellaneousView(cityDetailViewModel: CityDetailViewModel)
                        HStack {
                            Spacer()
                            Text("Data provided by ****")
                                .font(.caption2)
                            Spacer()
                        }
                        .padding()
                    }
                    .padding(.horizontal, 24)
                }) 
            }) 
            .edgesIgnoringSafeArea(.top)
            .navigationBarTitle(cityDetailViewModel.cityDetail.name, displayMode: .inline)
            .navigationBarHidden(true)
        } 
    }
}

struct CityDetailView_Previews: PreviewProvider {
    static var previews: some View {
        CityDetailView(cityDetailViewModel: CityDetailViewModel())
    }
}

image of the view containing the list:包含列表的视图的图像:

包含列表的视图

image of the parent view:父视图的图像:

父视图

You're embedding a scrolling view ( List ) inside another scrolling view ( ScrollView ), which is always going to have some inherent difficulties (and potentially strange UI/UX consequences with scrolling).您将滚动视图 ( List ) 嵌入到另一个滚动视图 ( ScrollView ) 中,这总是会遇到一些固有的困难(并且滚动可能会产生奇怪的 UI/UX 后果)。

Because the CityDetailCategoryView doesn't have any inherent height besides its header (the rest can be collapsed into the ScrollView ), it collapses to zero.因为CityDetailCategoryView除了 header (rest 可以折叠到ScrollView中)之外没有任何固有高度,因此它折叠为零。

You can use frame to set a height for it, as shown in this simplified example (if you remove the frame call, it behaves the same as yours):您可以使用frame为其设置高度,如下面的简化示例所示(如果您删除frame调用,它的行为与您的相同):

var items = [1,2,3,4,5,6]

struct ContentView: View {
    var body: some View {
        ScrollView {
            ForEach(items, id: \.self) { item in
                Text("item")
            }
            List {
                ForEach(items, id: \.self) { item in
                    Text("item")
                }
            }.frame(height: 200) //<-- here
            Text("last item")
        }
    }
}

That being said, I think you'll still run the risk of funny scrolling behavior.话虽如此,我认为您仍然会冒有趣的滚动行为的风险。

My suggestion would be to rearchitect your views so that you have one list or scrolling view for all of the content.我的建议是重新设计您的视图,以便为所有内容提供一个列表或滚动视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM