简体   繁体   中英

How to change the screen background color in SwiftUI mac app?

I have a following code:

    var body: some View {
        NavigationView {
            List {
                ForEach(users) { user in
                    HStack {
                        NetworkImage(
                            url: URL(string: "https://via.placeholder.com/100x100")
                        ).frame(width: 50, height: 50).cornerRadius(8.0)
                        Text(user.name)
                    }
                }
            }
            .navigationTitle("Users")
        }.onAppear {
            self.getUsers()
        }
    }

Now, how do I remove this translucent background in list view and use solid color like white?

在此处输入图像描述

You can us listRowBackground() modifier to apply colours the List.

List {
    ForEach(0..<10) {
        Text("Row \($0)")
    }
    .listRowBackground(Color.red)
}

Above is the Simple ListView and for your code ==

var body: some View {
        NavigationView {
            List {
                ForEach(users) { user in
                    HStack {
                        NetworkImage(
                            url: URL(string: "https://via.placeholder.com/100x100")
                        ).frame(width: 50, height: 50).cornerRadius(8.0)
                        Text(user.name)
                    }
                }.listRowBackground(Color.red)
            }
            .navigationTitle("Users")
        }.onAppear {
            self.getUsers()
        }
    }

I hope this would work for you.

Late answer...

Have you tried to embed your List in a ZStack ? And applying a Color view before the List?

var body: some View {
            NavigationView {
                ZStack {
                    Color.white.edgesIgnoringSafeArea(.all) //<--- HERE!
                    List {
                        ForEach(users) { user in
                            HStack {
                                NetworkImage(
                                    url: URL(string: "https://via.placeholder.com/100x100")
                                ).frame(width: 50, height: 50).cornerRadius(8.0)
                                Text(user.name)
                            }
                        }
                    }
                    .navigationTitle("Users")
                }
            }.onAppear {
                self.getUsers()
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM