SwiftUI 列表颜色背景
SwiftUI List color background
如果列出静态项目,我无法更改视图的背景颜色。这是我的代码:
NavigationView {
ZStack {
Color("AppBackgroundColor").edgesIgnoringSafeArea(.all)
List {
Section(header: Text("Now in theaters")) {
ScrollMovies(type: .currentMoviesInTheater)
}
Section(header: Text("Popular movies")) {
ScrollMovies(type: .popularMovies)
}
}.listStyle(.grouped)
}
}
因为您要求更改视图的背景颜色,
你可以使用 .colorMultiply()
。
代码:
var body: some View {
VStack {
ZStack {
List {
Section(header: Text("Now in theaters")) {
Text("Row1")
}
Section(header: Text("Popular movies")) {
Text("Row2")
}
/*Section(header: Text("Now in theaters")) {
ScrollMovies(type: .currentMoviesInTheater)
}
Section(header: Text("Popular movies")) {
ScrollMovies(type: .popularMovies)
} */
}.listStyle(.grouped)
}.colorMultiply(Color.yellow)
}
}
输出:
您可以为列表中的项目提供修饰符
NavigationView {
ZStack {
Color("AppBackgroundColor").edgesIgnoringSafeArea(.all)
List {
Section(header: Text("Now in theaters")) {
ScrollMovies(type: .currentMoviesInTheater)
.listRowBackground(Color.blue) // << Here
}
Section(header: Text("Popular movies")) {
ScrollMovies(type: .popularMovies)
.listRowBackground(Color.green) // << And here
}
}.listStyle(.grouped)
}
}
目前最简单的方法就是使用 UIAppearance 代理。 SwiftUI 还很年轻,所以苹果还没有完全正确地实现一些功能。
UITableView.appearance().backgroundColor = .red
@State var users: [String] = ["User 1",
"User 2",
"User 3",
"User 4"]
init(){
UITableView.appearance().backgroundColor = .red
UITableViewCell.appearance().backgroundColor = .red
UITableView.appearance().tableFooterView = UIView()
}
var body: some View {
List(users, id: \.self){ user in
Text(user)
}
.background(Color.red)
}
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())
所有 SwiftUI 的 List
均由 iOS 中的 UITableView
支持。所以您需要更改 tableView 的背景颜色。您将其设置为 clear
,以便其他 view
将在其下方可见:
struct ContentView: View {
init(){
UITableView.appearance().backgroundColor = .clear
}
var body: some View {
Form {
Section(header: Text("First Section")) {
Text("First cell")
}
Section(header: Text("Second Section")) {
Text("First cell")
}
}
.background(Color.yellow)
}
}
现在您可以使用您想要的任何背景(包括所有Color
)
注意那些顶部和底部的白色区域是安全区域你可以使用.edgesIgnoringSafeArea()
修饰符来获得摆脱他们。
另请注意, List
与 .listStyle(GroupedListStyle())
修饰符可以替换为简单的 Form
。但请记住,SwiftUI 控件的行为因它们的封闭视图而异。
此外您可能还想将 UITableViewCell
的背景颜色设置为 clear
以及普通表格视图
对于 macOS 上的 SwiftUI 这可行:
extension NSTableView {
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
// set the background color of every list to red
backgroundColor = .red
}
}
它将每个列表的背景颜色设置为红色(在该示例中)。
List
不能“画”。请改用以下内容:
项目列表:
ForEach(items) { item in
HStack {
Text(item)
}
}.background(Color.red)
可滚动的项目列表:
ScrollView {
ForEach(items) { item in
HStack {
Text(item)
}
}.background(Color.red)
}
你的情况:
VStack { // or HStack for horizontal alignment
Section(header: Text("Now in theaters")) {
ScrollMovies(type: .currentMoviesInTheater)
}
Section(header: Text("Popular movies")) {
ScrollMovies(type: .popularMovies)
}
}.background(Color.red)
使用UITableView.appearance().backgroundColor
可以影响应用中所有列表控件的背景颜色。如果您只想影响一个特定的视图,作为一种解决方法,您可以将其设置为 onAppear 并将其设置回默认的 onDisappear。
import SwiftUI
import PlaygroundSupport
struct PlaygroundRootView: View {
@State var users: [String] = ["User 1",
"User 2",
"User 3",
"User 4"]
var body: some View {
Text("List")
List(users, id: \.self){ user in
Text(user)
}
.onAppear(perform: {
// cache the current background color
UITableView.appearance().backgroundColor = UIColor.red
})
.onDisappear(perform: {
// reset the background color to the cached value
UITableView.appearance().backgroundColor = UIColor.systemBackground
})
}
}
PlaygroundPage.current.setLiveView(PlaygroundRootView())
聚会有点晚了,但这可能会有所帮助。我使用以下组合:
- 设置
UITableView.appearance
外观
- 设置
UITableViewCell.appearance
外观
- 设置
List
listRowBackground
修饰符。
设置外观会影响整个应用,因此您可能需要将其重置为适用的其他值。
示例代码:
struct ContentView: View {
var body: some View {
let items = ["Donald", "Daffy", "Mickey", "Minnie", "Goofy"]
UITableView.appearance().backgroundColor = .clear
UITableViewCell.appearance().backgroundColor = .clear
return ZStack {
Color.yellow
.edgesIgnoringSafeArea(.all)
List {
Section(header: Text("Header"), footer: Text("Footer")) {
ForEach(items, id: \.self) { item in
HStack {
Image(systemName: "shield.checkerboard")
.font(.system(size: 40))
Text(item)
.foregroundColor(.white)
}
.padding([.top, .bottom])
}
.listRowBackground(Color.orange))
}
.foregroundColor(.white)
.font(.title3)
}
.listStyle(InsetGroupedListStyle())
}
}
}
如果列出静态项目,我无法更改视图的背景颜色。这是我的代码:
NavigationView {
ZStack {
Color("AppBackgroundColor").edgesIgnoringSafeArea(.all)
List {
Section(header: Text("Now in theaters")) {
ScrollMovies(type: .currentMoviesInTheater)
}
Section(header: Text("Popular movies")) {
ScrollMovies(type: .popularMovies)
}
}.listStyle(.grouped)
}
}
因为您要求更改视图的背景颜色,
你可以使用 .colorMultiply()
。
代码:
var body: some View {
VStack {
ZStack {
List {
Section(header: Text("Now in theaters")) {
Text("Row1")
}
Section(header: Text("Popular movies")) {
Text("Row2")
}
/*Section(header: Text("Now in theaters")) {
ScrollMovies(type: .currentMoviesInTheater)
}
Section(header: Text("Popular movies")) {
ScrollMovies(type: .popularMovies)
} */
}.listStyle(.grouped)
}.colorMultiply(Color.yellow)
}
}
输出:
您可以为列表中的项目提供修饰符
NavigationView {
ZStack {
Color("AppBackgroundColor").edgesIgnoringSafeArea(.all)
List {
Section(header: Text("Now in theaters")) {
ScrollMovies(type: .currentMoviesInTheater)
.listRowBackground(Color.blue) // << Here
}
Section(header: Text("Popular movies")) {
ScrollMovies(type: .popularMovies)
.listRowBackground(Color.green) // << And here
}
}.listStyle(.grouped)
}
}
目前最简单的方法就是使用 UIAppearance 代理。 SwiftUI 还很年轻,所以苹果还没有完全正确地实现一些功能。
UITableView.appearance().backgroundColor = .red
@State var users: [String] = ["User 1",
"User 2",
"User 3",
"User 4"]
init(){
UITableView.appearance().backgroundColor = .red
UITableViewCell.appearance().backgroundColor = .red
UITableView.appearance().tableFooterView = UIView()
}
var body: some View {
List(users, id: \.self){ user in
Text(user)
}
.background(Color.red)
}
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())
所有 SwiftUI 的 List
均由 iOS 中的 UITableView
支持。所以您需要更改 tableView 的背景颜色。您将其设置为 clear
,以便其他 view
将在其下方可见:
struct ContentView: View {
init(){
UITableView.appearance().backgroundColor = .clear
}
var body: some View {
Form {
Section(header: Text("First Section")) {
Text("First cell")
}
Section(header: Text("Second Section")) {
Text("First cell")
}
}
.background(Color.yellow)
}
}
现在您可以使用您想要的任何背景(包括所有Color
)
注意那些顶部和底部的白色区域是安全区域你可以使用.edgesIgnoringSafeArea()
修饰符来获得摆脱他们。
另请注意, List
与 .listStyle(GroupedListStyle())
修饰符可以替换为简单的 Form
。但请记住,SwiftUI 控件的行为因它们的封闭视图而异。
此外您可能还想将 UITableViewCell
的背景颜色设置为 clear
以及普通表格视图
对于 macOS 上的 SwiftUI 这可行:
extension NSTableView {
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
// set the background color of every list to red
backgroundColor = .red
}
}
它将每个列表的背景颜色设置为红色(在该示例中)。
List
不能“画”。请改用以下内容:
项目列表:
ForEach(items) { item in
HStack {
Text(item)
}
}.background(Color.red)
可滚动的项目列表:
ScrollView {
ForEach(items) { item in
HStack {
Text(item)
}
}.background(Color.red)
}
你的情况:
VStack { // or HStack for horizontal alignment
Section(header: Text("Now in theaters")) {
ScrollMovies(type: .currentMoviesInTheater)
}
Section(header: Text("Popular movies")) {
ScrollMovies(type: .popularMovies)
}
}.background(Color.red)
使用UITableView.appearance().backgroundColor
可以影响应用中所有列表控件的背景颜色。如果您只想影响一个特定的视图,作为一种解决方法,您可以将其设置为 onAppear 并将其设置回默认的 onDisappear。
import SwiftUI
import PlaygroundSupport
struct PlaygroundRootView: View {
@State var users: [String] = ["User 1",
"User 2",
"User 3",
"User 4"]
var body: some View {
Text("List")
List(users, id: \.self){ user in
Text(user)
}
.onAppear(perform: {
// cache the current background color
UITableView.appearance().backgroundColor = UIColor.red
})
.onDisappear(perform: {
// reset the background color to the cached value
UITableView.appearance().backgroundColor = UIColor.systemBackground
})
}
}
PlaygroundPage.current.setLiveView(PlaygroundRootView())
聚会有点晚了,但这可能会有所帮助。我使用以下组合:
- 设置
UITableView.appearance
外观 - 设置
UITableViewCell.appearance
外观 - 设置
List
listRowBackground
修饰符。
设置外观会影响整个应用,因此您可能需要将其重置为适用的其他值。
示例代码:
struct ContentView: View {
var body: some View {
let items = ["Donald", "Daffy", "Mickey", "Minnie", "Goofy"]
UITableView.appearance().backgroundColor = .clear
UITableViewCell.appearance().backgroundColor = .clear
return ZStack {
Color.yellow
.edgesIgnoringSafeArea(.all)
List {
Section(header: Text("Header"), footer: Text("Footer")) {
ForEach(items, id: \.self) { item in
HStack {
Image(systemName: "shield.checkerboard")
.font(.system(size: 40))
Text(item)
.foregroundColor(.white)
}
.padding([.top, .bottom])
}
.listRowBackground(Color.orange))
}
.foregroundColor(.white)
.font(.title3)
}
.listStyle(InsetGroupedListStyle())
}
}
}