SwiftUI 列表的透明背景——iOS14 中的行为变化

lp1*_*756 5 swiftui ios14

我有一个带有背景的 SwiftUI 列表。在 iOS 13 中,我通过在 init() 中设置 UITableView 属性,成功使列表透明,以便背景显示出来。在 iOS 14 中,行为发生了变化。下面的代码片段显示了初始化设置。我已确认此提取的代码片段在 iOS 13 中按预期工作(背景通过列表显示),但在 iOS 14 中,列表中填充的行会阻挡背景,就好像背景是白色且不清晰一样。

还有其他人看过这个吗?是否有另一种方法可以使列表透明并且适用于 iOS 13 和 14?

struct RecorderList: View {
    init(){
        UITableView.appearance().backgroundColor = .clear
        UITableViewCell.appearance().backgroundColor = .clear
        UINavigationBar.appearance().largeTitleTextAttributes = [
            .foregroundColor: UIColor.purple,
            .font: UIFont(name:"Papyrus", size: 40) ?? UIFont.systemFont(ofSize:40)]
    }
    
    var body: some View {
        
        NavigationView {
            ZStack (alignment: .top){
                Image("background")
                    .resizable()
                    .scaledToFit()
                List {
                    Text("One")
                        .font(.title)
                        .background(Color.clear)
                    Text("Two")
                        .font(.title)
                        .background(Color.clear)
                    Text("Three")
                        .font(.title)
                        .background(Color.clear)
                    Text("Four")
                        .font(.title)
                        .background(Color.clear)
                    Text("Five")
                        .font(.title)
                        .background(Color.clear)

                    }
                }
                .navigationBarTitle("Recorders")
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

paw*_*222 10

您可以使用listRowBackground

var body: some View {
    NavigationView {
        ZStack(alignment: .top) {
            Image("background")
                .resizable()
                .scaledToFit()
            List {
                Group {
                    Text("One")
                    Text("Two")
                    Text("Three")
                    Text("Four")
                    Text("Five")
                }
                .font(.title)
                .listRowBackground(Color.clear)
            }
        }
        .navigationBarTitle("Recorders")
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,通过放入视图,Group您可以分别对每个视图应用修改器。