【问题标题】:Change background color of SwiftUI List View更改 SwiftUI 列表视图的背景颜色
【发布时间】:2021-03-11 16:00:22
【问题描述】:

我在 SwiftUI 中有一个视图,但没有改变背景颜色的方法。我的主要代码是:

var body: some View {
        ZStack {
            Color(red: 0.17, green: 0.24, blue: 0.31)
                .edgesIgnoringSafeArea(.all)
            VStack{
                NavigationLink(destination: WelcomeView(),isActive: $isShowWelcomeView){}
                List(Modules.Modules) { module in
                    NavigationLink(destination: ModuleView(moduleName: module.name)){
                        ModuleRow(mod: module)
                    }
                }
                .navigationBarItems(trailing:
                            Button(action: {logout()}) {
                                Text("Logout")
                            })
                .navigationBarTitle("CORSO")
                .navigationBarBackButtonHidden(true)
            }
        }
    }

我列表的通用元素是自定义元素,代码是(存储在另一个文件类中):

var body: some View {
    ZStack {
        Color(red: 0.17, green: 0.24, blue: 0.31)
                    .cornerRadius(12)
        HStack {
            VStack(alignment: .leading, spacing: 8) {
                Text(mod.name)
                    .font(.title)
                    .fixedSize(horizontal: false, vertical: true)
                    .foregroundColor(.yellow)
                Text(mod.summary)
                    .font(.caption)
                    .foregroundColor(.yellow)
            }
            Spacer()
            Image(systemName: mod.imageName)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 50, height: 50, alignment: .center)
                .padding()
                .foregroundColor(.yellow)
        }
        .padding()
        .background(Color(red: 0.17, green: 0.24, blue: 0.31))
        .listRowBackground(Color(red: 0.17, green: 0.24, blue: 0.31))
    }
    .fixedSize(horizontal: false, vertical: true)
    .shadow(color: Color.black.opacity(0.2), radius: 5, x: 0, y: 2)
}

但是,不幸的是,背景颜色仍然是“白色”并且没有改变。想要的颜色是:Color(red: 0.17, green: 0.24, blue: 0.31)

这是一个运行时结果: simulator

请帮帮我!

【问题讨论】:

    标签: swift xcode swiftui ios14


    【解决方案1】:

    如果您可以不使用 LazyVStack 或 VStack,则可以轻松解决此问题,但这只是以防万一您需要使用 List。 请注意,这肯定不是生产就绪代码,而是一个可能的解决方案或方向,如下所示。

    struct ListBackgroundColorer: UIViewRepresentable {
        public func makeUIView(context: Context) -> UIView {
            
            let view = UIView()
            DispatchQueue.main.async {
                view.superview?.superview?.superview?.superview?.superview?.subviews.first(where: {NSStringFromClass(type(of: $0)) == "_UISystemBackgroundView"})?.subviews[0].backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0)
            }
            return view
        }
        
        public func updateUIView(_ uiView: UIView, context: Context) {}
    }
    

    现在使用它作为背景。

    .background(ListBackgroundColorer())
    

    结果:

    感谢 Asperi 的启发。

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2019-10-19
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2019-11-29
      相关资源
      最近更新 更多