繁体   English   中英

SwiftUI 如何使列表和部分透明

[英]SwiftUI how to make List and Section transparent

我正在尝试制作一个看起来像这样的列表:

在此处输入图片说明

但是,无论我尝试什么,我都无法弄清楚如何使 List 和 Sections 都具有.clear背景,目前看起来像这样:

在此处输入图片说明

我曾尝试将.background(Color.clear)添加到 List 和 Section,但没有成功。 当我使用透明以外的任何其他颜色时,它确实会改变得很好。 但似乎在背景颜色“后面”有一些东西使它仍然在下面显示白色和灰色......

如何更改此列表以使其完全清楚?

这是我目前失败的尝试:

var body: some View {
        NavigationView {
            List {
                ForEach(uniqueEntries, id: \.self) { category in
                    Section(header: HStack {
                        Text(category)
                            .font(.headline)
                            .foregroundColor(.white)
                            .padding()
                        Spacer()
                    }
                        .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
                        .background(Color.clear)
                    ) {
                        ForEach(self.entriesCollatedByCategory[category]!) { entry in
                            ExpenseRow(entry: entry)
                                .listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
                                .listRowBackground(Color.clear)
                            
                                
                        }
                    }
                }
            }
            .padding()
            .background(Image("tempGradientBackground")
                            .resizable()
                            .aspectRatio(contentMode: .fill))
            .navigationBarTitle(Text("Budget"))
        }.background(Color.clear)
    }

但似乎在背景颜色“后面”有一些东西使它仍然在下面显示白色和灰色......

尝试删除UITableViewUITableViewCell背景颜色以及:

UITableView.appearance().backgroundColor = .clear
UITableViewCell.appearance().backgroundColor = .clear

在init里面添加外观样式

init() {
    UITableViewCell.appearance().backgroundColor = .clear
    UITableView.appearance().backgroundColor = .clear
    UITableView.appearance().sectionIndexBackgroundColor = .clear
}

并且您还需要更改ListStyle以使 SectionBackground 透明

.listStyle(GroupedListStyle())

编辑:在您的情况下使用 InsetGroupedList 更好。 感谢 pawello22222

.listStyle(InsetGroupedListStyle())

暂无
暂无

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

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