【发布时间】:2022-01-03 17:16:21
【问题描述】:
我想将背景白色更改为 SwiftUI 中列表的另一种颜色。
struct ListCustom: View {
var body: some View {
ContentView1()
}
}
struct ContentView1: View {
@State var items = Array(1...20)
init() {
UITableView.appearance().backgroundColor = UIColor.init(.red)
UITableViewCell.appearance().backgroundColor = UIColor.init(.red)
let color = UIView()
color.backgroundColor = UIColor.clear
UITableViewCell.appearance().selectedBackgroundView = color
}
var idetifca = UUID()
var body: some View {
VStack {
Button("Shuffle") {
self.items.shuffle()
}
List(items, id: \.self) {_ in
Screen(Array1: $items).listRowBackground(Color.green)
}.id(idetifca).listStyle(PlainListStyle()).background(Color.blue)
.listRowBackground(Color.blue)
}
}
}
struct Screen: View {
@Binding var Array1 : [Int]
var body: some View {
Group {
if self.Array1.count > 0{
SectionTitleUI(titleStrng: "Testing", rightTitleString: "", countShow: 0) {}
ForEach(self.Array1.indices, id: \.self) { notification in
NotificationView(notificationInfo: self.Array1[notification])
.listRowBackground(Color.red)
}
}
}
.navigationBarHidden(false)
}
}
import SwiftUI
struct NotificationView: View {
@State var notificationInfo = 0
var body: some View {
VStack {
HStack(alignment:.top) {
VStack(alignment: .leading, spacing: 10){
HStack(alignment:.top){
Text("Text").onAppear{
print("rrrr")
}
.foregroundColor(.black)
}
.fixedSize(horizontal: false, vertical: true)
Group{
Text("Text111")
}
.font(.system(size: 15.0))
.foregroundColor(.gray)
}
.frame(maxWidth: .infinity ,alignment: .topLeading)
}
.padding(.all)
Divider()
.background(Color.white)
.offset(x: 0, y:0)
}.background(notificationInfo == 0 ? Color.red : Color.clear).listRowBackground(Color.red)
}
}
struct SectionTitleUI: View {
var titleStrng = "2"
var rightTitleString = "3"
var countShow = 4
var comeFromPublicRide = false
var changeFontSize = false
var action: (() -> Void)?
var body: some View {
VStack(alignment: .leading) {
HStack {
Text(titleStrng).font(.system(size:changeFontSize == true ? 15 : 18, weight: .bold, design: .default))
.foregroundColor(.white)
.padding(.leading)
.fixedSize(horizontal: false, vertical: true)
if countShow != 0{
Text("\(countShow)").font(.system(size: 16, weight: .bold, design: .default))
.foregroundColor(.gray)
.padding(.leading,0)
}
Spacer()
}.padding(5)
.frame(height: 45)
.background(Color.red)
}.listRowBackground(Color.red)
}
}
输出:-
当用户运行应用程序时,列表颜色未在所有单元格中完美设置,自动显示白色。当用户滚动列表时,自动改变我设置的颜色。
问题:-如何一次性更改完整列表颜色?
有人可以向我解释如何更改列表颜色,我已经尝试使用上面的代码但还没有结果。
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
标签: list swiftui background-color swiftui-list