19

How can I make the text background above the navigation bar translucent so that it looks like the text and navigation bar are the same object?

VStack(spacing: 0) {
    Text("Test")
        .padding(.top, 9.5)
        .padding(.bottom, 8)
        .frame(minWidth: 0, maxWidth: .infinity)
        .background(Color.gray) // I used a custom color set in the screenshot
        .font(.footnote)
    NavigationView {
        List {
            Text("Name")
            Text("Name")
            Text("Name")
        } .listStyle(GroupedListStyle())

        .navigationBarTitle(Text(""), displayMode: .inline)
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(
            leading:
            Button("Cancel") {
                // self.presentationMode.wrappedValue.dismiss()
            },
            trailing:
            Button("Done") {

            }.disabled(true)
        )
    }
}

enter image description here

Mattis Schulte
  • 639
  • 1
  • 10
  • 18
  • Does this answer your question? [How to create a color with an alpha value using SwiftUI?](https://stackoverflow.com/questions/56822573/how-to-create-a-color-with-an-alpha-value-using-swiftui) – elight May 02 '20 at 18:31

1 Answers1

39

SwiftUI's Color has an opacity() function that returns another Color with the given opacity. An opacity of 1.0 would be the same color, while an opacity of 0.0 would be completely clear.

For example, if you wanted to have the color be between completely opaque and completely clear, change:

Text("Test")
        .padding(.top, 9.5)
        .padding(.bottom, 8)
        .frame(minWidth: 0, maxWidth: .infinity)
        .background(Color.gray)
        .font(.footnote)

To:

Text("Test")
        .padding(.top, 9.5)
        .padding(.bottom, 8)
        .frame(minWidth: 0, maxWidth: .infinity)
        .background(Color.gray.opacity(0.5)) //Here is where we use the opacity
        .font(.footnote)

Source: https://developer.apple.com/documentation/swiftui/color

David Chopin
  • 2,780
  • 2
  • 19
  • 40