4

I want to set a image header for list in swiftui. The effect I want is shown in the figure below:

enter image description here

My code is as bellow:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Section() {
                    VStack() {
                        Image("HeadImage")
                            .resizable()
                            .frame(width: 50, height: 50)
                        Text("Test word")
                            .foregroundColor(Color.red)
                    }
                    .frame(minWidth: 0, maxWidth: .infinity)
                    .frame(height: 150)
                }
                ForEach((0..<4), id: \.self) { index in
                    Section {
                        NavigationLink(destination: Text("aaa")) {
                            Label("Buttons", systemImage: "capsule")
                        }
                        NavigationLink(destination: Text("aaa")) {
                            Label("Colors", systemImage: "paintpalette")
                        }
                        NavigationLink(destination: Text("aaa")) {
                            Label("Controls", systemImage: "slider.horizontal.3")
                        }
                    }
                }
            }
            .navigationBarTitle("SwiftUI")
            .navigationBarTitleDisplayMode(.inline)
        }
        .accentColor(.accentColor)
    }
}

The result is as bellow, the header view has a white background:

enter image description here

I find a method to set the image row backgroud color as bellow:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                Section() {
                    VStack() {
                        Image("HeadImage")
                            .resizable()
                            .frame(width: 50, height: 50)
                        Text("Test word")
                            .foregroundColor(Color.red)
                        
                    }
                    .frame(minWidth: 0, maxWidth: .infinity)
                    .frame(height: 150)
                    .listRowInsets(EdgeInsets())
                    // rgb get from screenshot of List in Sketch
                    .background(Color(red: 242/255, green: 242/255, blue: 247/255))
                }
                ForEach((0..<4), id: \.self) { index in
                    Section {
                        NavigationLink(destination: Text("aaa")) {
                            Label("Buttons", systemImage: "capsule")
                        }
                        NavigationLink(destination: Text("aaa")) {
                            Label("Colors", systemImage: "paintpalette")
                        }
                        NavigationLink(destination: Text("aaa")) {
                            Label("Controls", systemImage: "slider.horizontal.3")
                        }
                    }
                }
            }
            .navigationBarTitle("SwiftUI")
            .navigationBarTitleDisplayMode(.inline)
        }
        .accentColor(.accentColor)
    }
}

This can make the image row to same background color with the List.

But this way is to set a fixed color, which is the RGB value of the background color of the List obtained by using the color finder in Sketch. It is not a very beautiful method. Is there a more elegant way to set the background color of the image row to be the same as the background color of the List?

1
  • I am a bit lost in your question here. Your description, image, and question title do not match with each other at all. Can you edit and let us know again what do you want exactly? Commented Jul 22, 2022 at 3:18

1 Answer 1

5

It can be done making list row background transparent, like

Section() {
    VStack() {
        Image("HeadImage")
            .resizable()
            .frame(width: 50, height: 50)
        Text("Test word")
            .foregroundColor(Color.red)

    }
    .frame(minWidth: 0, maxWidth: .infinity)
    .frame(height: 150)
    .listRowInsets(EdgeInsets())
    .listRowBackground(Color.clear)     // << here !!

Tested with Xcode 13.4 / iOS 15.5

demo

Sign up to request clarification or add additional context in comments.

2 Comments

This works nice but not when there is a shadow going outside the bounds of the before visible grouped clip shape. Any ideas how to get rid of that? In other words the rowInset = .none and frame.maxWidth = .infinity don't ensure the full width.
Found the solution. Rather a hack but works: Creating an empty Section and putting the views inside the header of that section Section() { VStack() {}.frame(maxWidth: .infinity).listRowInsets(.none) .listRowBackground(Color.clear)} header: { YourView() }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.