【问题标题】:Modify the background colour of a List in SwiftUI?在 SwiftUI 中修改 List 的背景颜色?
【发布时间】: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


    【解决方案1】:

    删除ContentView1init 可以解决此问题。

    问题是由您的代码设置selectedBackgroundView 引起的,但无论如何您都不需要init,因为它仍然可以按您的预期运行。

    【讨论】:

    • 好的,非常感谢@George。我将删除代码并尽快更新您。
    【解决方案2】:

    给你!您只是在任何地方设置了列表行背景颜色...我认为您因此而感到困惑。

    您只需在 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()
        }
    }
    

    有什么问题可以问我。

    【讨论】:

    • 谢谢@Alexander,您的回答也对我有用。
    猜你喜欢
    • 2019-10-24
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2020-10-11
    • 2021-07-25
    • 2020-05-15
    相关资源
    最近更新 更多