0
votes

I have a ScrollView displaying a list of items in a VStack instead of using a List. I'd like to replicate the behaviour of a .large navigation bar title where the background color uses the .systemGroupedBackground

This is the behaviour of a List that I'd like to have. The background of the Summary is transparent.

enter image description here

This is the behaviour I get when using a ScrollView + VStack. Note the white background of the large navigation bar.

enter image description here

The code to reproduce both is

import SwiftUI

struct MyContentView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: 8) {
                    Text("One")
                        .roundedContainer()
                    Text("Two")
                        .roundedContainer()
                }
                .padding()
            }
            .background(Color(.systemGroupedBackground))
            .navigationBarTitle("Summary", displayMode: .large)
        }
    }
}

struct MyListContentView: View {
    var body: some View {
        NavigationView {
            List{
                Text("One")
                    .padding()
                Text("Two")
                    .padding()
            }
            .listStyle(InsetGroupedListStyle())
            .navigationBarTitle("Summary", displayMode: .large)
        }
    }
}


extension View {
    func roundedContainer() -> some View {
        self
            .frame(maxWidth: .infinity)
            .padding(.vertical)
            .background(Color.white)
            .cornerRadius(10)
    }
}

struct MyContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            MyContentView()
            MyListContentView()
        }
    }
}
1

1 Answers

0
votes

You could introduce a custom ViewModifier as described here.

Working example:

import SwiftUI

struct NavigationBarModifier: ViewModifier {
    var backgroundColor: UIColor?

    init(backgroundColor: UIColor?) {
        self.backgroundColor = backgroundColor
    }

    func body(content: Content) -> some View {
        ZStack{
            content
            VStack {
                GeometryReader { geometry in
                    Color(self.backgroundColor ?? .clear)
                        .frame(height: geometry.safeAreaInsets.top)
                        .edgesIgnoringSafeArea(.top)
                    Spacer()
                }
            }
        }
    }
}

extension View {

    func navigationBarColor(_ backgroundColor: UIColor?) -> some View {
        self.modifier(NavigationBarModifier(backgroundColor: backgroundColor))
    }

}

struct MyContentView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: 8) {
                    Text("One")
                        .roundedContainer()
                    Text("Two")
                        .roundedContainer()
                }
                .padding()
            }
            .background(Color(.systemGroupedBackground))
            .navigationBarTitle("Summary", displayMode: .large)
            .navigationBarColor(.systemGroupedBackground)
        }
    }
}