我想將背景白色更改為 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)
}
}
輸出:-

當用戶運行應用程式時,所有單元格中的串列顏色設定不完美,自動顯示白色。當用戶滾動串列時,我設定的顏色會自動更改。
問題:-如何一次性更改完整串列顏色?
有人可以向我解釋如何更改串列顏色,我已經嘗試使用上面的代碼但還沒有結果。
任何幫助將不勝感激。
提前致謝。
uj5u.com熱心網友回復:
洗掉ContentView1's 可以init解決問題。
問題是由您的代碼設定引起的selectedBackgroundView,但init無論如何您都不需要任何設定,因為它仍然可以按您的預期運行。
uj5u.com熱心網友回復:
你去吧!您只需在任何地方設定串列行背景顏色...我想您因此感到困惑。
您只需要在 Screen 結構中設定串列行背景顏色。希望你明白我改變了什么。
import SwiftUI
struct ContentView: View {
@State var items = Array(1...20)
var idetifca = UUID()
var body: some View {
VStack {
Button("Shuffle") {
self.items.shuffle()
}
List(items, id: \.self) {_ in
Screen(Array1: $items)
}
.id(idetifca)
.listStyle(PlainListStyle())
}
}
}
struct Screen: View {
@Binding var Array1 : [Int]
var body: some View {
Group {
if self.Array1.count > 0{
SectionTitleUI(titleStrng: "Testing", rightTitleString: "", countShow: 0) {}
.listRowBackground(Color.red)
ForEach(self.Array1.indices, id: \.self) { notification in
NotificationView(notificationInfo: self.Array1[notification])
}
.listRowBackground(Color.red)
}
}
.navigationBarHidden(false)
}
}
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)
}
}
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)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
有什么需要問我的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/366584.html
下一篇:更新串列中的串列項(矩陣)
