5

I'm trying to use the Picker for a simple selection. Currently I'm using the .inline navigation bar for showing the titles. (here's where i got the example https://www.hackingwithswift.com/quick-start/swiftui/pickers-in-forms)

struct ContentView: View {
    var strengths = ["Mild", "Medium", "Mature"]

    @State private var selectedStrength = 0
    
    var body: some View {
        NavigationView {
            Form {
                Picker(selection: $selectedStrength, label: Text("Strength")) {
                    ForEach(0 ..< strengths.count) {
                        Text(self.strengths[$0])
                    }
                }
            }.navigationBarTitle("Parent Title", displayMode: .inline)
        }
    }
}

So my issue is that when I go into the picker, there is no title? Screenshot 1

When I try to set the inline title on the Picker, or ForEach or even the Texts inside the foreach, the Parent Title gets overridden...

Is there a nice way of fixing this problem or should i just go ahead and make my own picker (any good simple pickers our there)?

6
  • You need to set the navigationBarTitle for the Picker object as well. And of course the parent title gets overwritten. UINavigationBar doesn't show two titles. Commented Aug 21, 2020 at 16:34
  • 1
    @ClausJørgensen I tried that, it overrides the Parent Title and still doesn't show on the actual picker screen :( Commented Aug 21, 2020 at 16:37
  • 1
    Have you looked at stackoverflow.com/questions/58320934/… ? Commented Aug 21, 2020 at 16:38
  • @ClausJørgensen I just tried it and it worked...! thank you Commented Aug 21, 2020 at 16:49
  • Sadly it didn’t work for me in Xcode 12 beta 5. Commented Aug 21, 2020 at 17:36

1 Answer 1

-6

This is what I ended up doing (got the idea from Change Navigation Title of Picker in SwiftUI thanks to Claus Jørgensen):

struct ContentView: View {
    var strengths = ["Mild", "Medium", "Mature"]

    @State private var selectedStrength = 0

    var body: some View {
        NavigationView {
            Form {
                Picker(selection: $selectedStrength, label: Text("Strength")) {
                    ForEach(0 ..< strengths.count) {
                        Text(self.strengths[$0])
                    }.navigationBarTitle("Strength", displayMode: .inline)
                }.navigationBarTitle("Parent Title", displayMode: .inline)
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This will also change the parent title to "Strength" when the user taps on the back button.

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.