0

Is it possible to add background color to the top of a picker view? I'm specifically interested in the area from the top of the view down to just below the navigation bar. I have made all my app tab and navigation views with the same header appearance but haven't had any luck with the pickers.

I would like to set the picker backgrounds to look like this: I would like the background to look like this view

Here is a picker view that I would like to add background color. here is my picker

Here is what happens when adding call to DrawNavBox() just after the Picker (See code below). I need help pushing the green block to the top of the view. attempt to add header color to picker

The code below is what generates the Home Currency green header above

 var body: some View {

        GeometryReader { g in
            VStack {

                DrawNavBox(g: g) // draw green header

                Text("Your home currency is:")
                    .padding(.top, g.size.height * 0.10)

                Text("\(base.baseCur.baseCurrency) \(base.baseCur.baseCountry)")
                    .font(.body)
                    .fontWeight(.bold)
                    .foregroundColor(.white)
                    .padding(15)
                    .background(Color("Dark Green"))
                    .cornerRadius(20)
                    .padding(.trailing, g.size.width * 0.19)
                    .padding(.leading, g.size.width * 0.22)

                Form {
                    Section(header: Text("Select Home Currency")) {

                        Picker("Home Currency", selection: $gotBase) {
                           DrawNavBox(g: g)
                            ForEach(0 ..< currCountry.count, id: \.self) { item in
                                VStack(alignment: .leading) {

                                    Text(currCountry[item])
                                        .font(.subheadline)
                                        .padding(.leading, 25)

                                    HStack {
                                        Text(currSymbol[item])
                                        Text(currName[item])
                                    }
                                    .font(.caption)
                                    .padding(.leading, 25)
                                }
                            }

                        }
                    }
                }
            }
            .navigationBarTitle(Text("Home Currency"), displayMode: .inline)
            .navigationBarItems(trailing:
                                    Button(action: {
                // search for new base currency
                self.changeBaseCurrency()

            }) {
                Text ("Save").bold()
            }
            )}
    }
}

This is the code I have been using to set the navigation bar area green

struct DrawNavBox: View {

   var g: GeometryProxy

   var body: some View {

       ZStack (alignment: .top) {
           Color(.systemGreen)
               .frame(height: (g.safeAreaInsets.top), alignment: .top)
               .ignoresSafeArea()
       }
   }
}

Galen Smith
  • 299
  • 2
  • 14
  • On iOS 16 it does not navigate to select anymore but use popover menu style, so think if it is worth doing. – Asperi Jul 22 '22 at 19:37
  • That is what I have been afraid of--that the changes to navigation in iOS16 would impact something like this. I think I understand that you are suggesting (iOS15) use of one of the picker style options such as menuPickerStyle. Thanks – Galen Smith Jul 22 '22 at 20:39
  • This question has been asked and answered before. Check this out: [https://stackoverflow.com/questions/56923397/how-change-background-color-if-using-navigationview-in-swiftui](https://stackoverflow.com/questions/56923397/how-change-background-color-if-using-navigationview-in-swiftui) – Arcee Jul 23 '22 at 16:00

0 Answers0