SwiftUI 列表的透明背景——iOS14 中的行为变化
Transparent Background for SwiftUI Lists -- Behavior Change in 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")
}
}
}
您可以使用 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
中,您可以分别对每个视图应用修饰符。
由于某些原因,listRowBackgound
修饰符对我不起作用。我短期内要做的是
.listRowInsets(EdgeInsets()) // hack for ios14
.background(Color.black) // hack for 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")
}
}
}
您可以使用 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
中,您可以分别对每个视图应用修饰符。
由于某些原因,listRowBackgound
修饰符对我不起作用。我短期内要做的是
.listRowInsets(EdgeInsets()) // hack for ios14
.background(Color.black) // hack for ios14
然后调整填充。我希望其他人有更好的主意。