SwiftUI列表的透明背景——iOS14中的行为更改
我有一个有背景的快捷界面列表。在iOS 13中,通过在init()中设置UITableView属性,我成功地使列表透明,这样背景就会显示出来。在iOS 14中,行为发生了变化。下面的代码片段显示了init设置。我已经确认,这个提取的代码片段在iOS 13中可以正常工作(通过列表显示背景),但在iOS 14中,列表中填充的行会阻塞背景,就好像背景是白色的,不清晰一样 还有谁见过这个吗?是否有另一种方法可以使列表透明,同时适用于iOS 13和iOS 14SwiftUI列表的透明背景——iOS14中的行为更改,swiftui,ios14,Swiftui,Ios14,我有一个有背景的快捷界面列表。在iOS 13中,通过在init()中设置UITableView属性,我成功地使列表透明,这样背景就会显示出来。在iOS 14中,行为发生了变化。下面的代码片段显示了init设置。我已经确认,这个提取的代码片段在iOS 13中可以正常工作(通过列表显示背景),但在iOS 14中,列表中填充的行会阻塞背景,就好像背景是白色的,不清晰一样 还有谁见过这个吗?是否有另一种方法可以使列表透明,同时适用于iOS 13和iOS 14 struct RecorderList: V
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")
}
}
}
您可以使用
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")
}
}
另外,通过将视图放入
组
中,您可以分别对每个视图应用修改器。由于某些原因,listRowBackgound
修改器对我不起作用。我的短期打算是
.listRowInsets(EdgeInsets()) // hack for ios14
.background(Color.black) // hack for ios14
然后调整填充物。我希望其他人有更好的主意。谢谢你pawello2222——这很好地解决了问题。但有趣的是,在第一次尝试时,它并没有。它在我的精简示例中起作用,正如您在上面的修改一样,但在我的实际应用中,文本视图在NavigationLink块内实例化,然后在ForEach块内实例化,只是在文本上添加.listRowBackground不起作用。不过,在我在ForEach循环的NavigationLink中的单个文本视图周围添加了一个组块,然后将.listRowBackground()放在该组上之后,它确实起到了作用。再次感谢!应用
listRowBackground(Color.clear)
并没有奏效。为了使单元格背景和表视图背景清晰,您还需要设置UITableViewCell.appearance().backgroundColor=.clear
和UITableView.appearance().backgroundColor=.clear
-然后设置一个列表背景:.background(myBackColor.edgesIgnoringSafeArea(.all))
。有相同的问题。似乎,UITableViewCell.appearance().backgroundColor=…
在iOS 14中没有效果,我称之为bug。在模拟器iOS 14.5中仍然有相同的行为<代码>listRowBackground(..)也不起作用。