SwiftUI:使插图列表背景透明?
SwiftUI: Make inset List background transparent?
如何将 List
的灰色背景设置为完全透明,让红色透过?
struct ContentView: View {
var body: some View {
NavigationView {
ZStack {
Color.red
.ignoresSafeArea()
VStack {
HStack {
Text("Faux Title")
.font(.system(.largeTitle, design: .rounded))
.fontWeight(.heavy)
Spacer()
Button(action: {
// settings
}, label: {
Image(systemName: "gearshape.fill")
.font(.system(.title2))
})
}
.padding()
.navigationBarHidden(true)
List {
Text("One")
Text("Two")
Text("Three")
}
.listStyle(InsetGroupedListStyle())
}
}
}
}
}
在视图更改的 init() 中 UITableView.appearance
:
我们在 SwiftUI 中使用的最多的东西,例如 List 或 NavigationView,. .它们不是纯 SwiftUI,而是来自 UIKit,因此我们使用 UIKit 代码类型来更改外观。如果你记得我们应该在 UIKit 中使用这样的代码。当我们在 init 中使用 UITableView
时,我们正在访问 UITableView UIKit 的 class,这意味着此更改将应用于我们项目中的任何视图,它们是使用列表。
最好的方法就是按照你的方法,设置透明颜色,然后在body中从SwiftUI改变颜色!这是最方便的方法。
struct ContentView: View {
init() { UITableView.appearance().backgroundColor = UIColor.clear } // <<: here!
var body: some View {
NavigationView {
ZStack {
Color.red
.ignoresSafeArea()
VStack {
HStack {
Text("Faux Title")
.font(.system(.largeTitle, design: .rounded))
.fontWeight(.heavy)
Spacer()
Button(action: {
// settings
}, label: {
Image(systemName: "gearshape.fill")
.font(.system(.title2))
})
}
.padding()
.navigationBarHidden(true)
List {
Text("One")
Text("Two")
Text("Three")
}
.listStyle(InsetGroupedListStyle())
}
}
}
}
}
如何将 List
的灰色背景设置为完全透明,让红色透过?
struct ContentView: View {
var body: some View {
NavigationView {
ZStack {
Color.red
.ignoresSafeArea()
VStack {
HStack {
Text("Faux Title")
.font(.system(.largeTitle, design: .rounded))
.fontWeight(.heavy)
Spacer()
Button(action: {
// settings
}, label: {
Image(systemName: "gearshape.fill")
.font(.system(.title2))
})
}
.padding()
.navigationBarHidden(true)
List {
Text("One")
Text("Two")
Text("Three")
}
.listStyle(InsetGroupedListStyle())
}
}
}
}
}
在视图更改的 init() 中 UITableView.appearance
:
我们在 SwiftUI 中使用的最多的东西,例如 List 或 NavigationView,. .它们不是纯 SwiftUI,而是来自 UIKit,因此我们使用 UIKit 代码类型来更改外观。如果你记得我们应该在 UIKit 中使用这样的代码。当我们在 init 中使用 UITableView
时,我们正在访问 UITableView UIKit 的 class,这意味着此更改将应用于我们项目中的任何视图,它们是使用列表。
最好的方法就是按照你的方法,设置透明颜色,然后在body中从SwiftUI改变颜色!这是最方便的方法。
struct ContentView: View {
init() { UITableView.appearance().backgroundColor = UIColor.clear } // <<: here!
var body: some View {
NavigationView {
ZStack {
Color.red
.ignoresSafeArea()
VStack {
HStack {
Text("Faux Title")
.font(.system(.largeTitle, design: .rounded))
.fontWeight(.heavy)
Spacer()
Button(action: {
// settings
}, label: {
Image(systemName: "gearshape.fill")
.font(.system(.title2))
})
}
.padding()
.navigationBarHidden(true)
List {
Text("One")
Text("Two")
Text("Three")
}
.listStyle(InsetGroupedListStyle())
}
}
}
}
}