SwiftUI如何使列表和分区透明

3
我正在尝试制作一个类似于这样的列表:

enter image description here

无论我尝试什么,都无法弄清楚如何使列表和部分都具有.clear背景,目前看起来是这样的:

enter image description here

我已经尝试将.background(Color.clear)添加到列表和部分中,但没有成功。 当我使用除clear以外的任何颜色时,它都可以正常更改。但似乎在背景色之后有一些东西,使其仍然显示白色和灰色...

如何将此列表更改为完全透明?

这是我当前失败的尝试:

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)
    }
2个回答

3
但似乎有某种“背后的”背景色使其仍然显示白色和灰色...... 尝试同时移除UITableView和UITableViewCell的背景色:
UITableView.appearance().backgroundColor = .clear
UITableViewCell.appearance().backgroundColor = .clear

3
在init函数内加入外观样式。
init() {
    UITableViewCell.appearance().backgroundColor = .clear
    UITableView.appearance().backgroundColor = .clear
    UITableView.appearance().sectionIndexBackgroundColor = .clear
}

你需要同时更改ListStyle, 以使SectionBackground透明

.listStyle(GroupedListStyle())

编辑:在这种情况下最好使用InsetGroupedList。感谢pawello22222的建议。

.listStyle(InsetGroupedListStyle())

1
实际上可以使用.listStyle(InsetGroupedListStyle()) - 这应该更符合OP的要求(至少我是这么认为的)。 - pawello2222
@pawello2222 是的,这样好多了。谢谢!我会编辑它。 - davidev
非常感谢!这是我的第一个Swift应用程序,我不知道如何覆盖默认设置,所以这对我非常有帮助! - MLyck
从iOS 16开始,使用外观代理进行黑客攻击已不再奏效。 - rgeorge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接