SwiftUI列表的透明背景-iOS14中的行为更改

时间:2020-09-17 22:38:03

标签: swiftui ios14

我有一个带有背景的SwiftUI列表。在iOS 13中,通过在init()中设置UITableView属性,我成功地使List透明,从而可以显示背景。使用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")
            }
        }
}

2 个答案:

答案 0 :(得分:1)

您可以使用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")
    }
}

也可以通过将视图放入Group中来将修饰符分别应用于每个视图。

答案 1 :(得分:0)

由于某些原因,listRowBackgound修饰符对我不起作用。我短期内打算的是

.listRowInsets(EdgeInsets()) // hack for ios14
.background(Color.black) // hack for ios14

,然后调整填充。我希望其他人有更好的主意。