2

I have a Form view that sets the navigation bar title to "Settings" and a Picker view inside the Form view that sets the navigation bar title to "Options".

When navigating to Picker view, the navigation bar title does not get set to "Options", but the back button text does. Also, when navigating back to Form view, the navigation bar title is changed to "Options".

How can I have the navigation bar title be "Settings" while on Form view and "Options" while on Picker view? Thanks!

var body: some View {
    NavigationView {
        Form {
            Picker(selection: $currentSelection, label: Text("Options")) {
                ForEach(0 ..< options.count) {
                    Text(options[$0])
                }
            }
            .navigationBarTitle("Options", displayMode: .inline)
        }
        .navigationBarTitle("Settings", displayMode: .inline)
    }
}

enter image description here

Alex
  • 739
  • 1
  • 6
  • 18

1 Answers1

5

Put navigation title modifier inside Picker. Also as same Text view from Picker item is used to present selection in Form we need to make Options title conditional.

Here is a demo of approach. Tested with Xcode 12.4 / iOS 14.4

demo

Form {
    Picker(selection: $currentSelection, label: Text("Options")) {
        ForEach(0 ..< options.count) {
            if $0 != currentSelection {
               Text(options[$0])
                .navigationBarTitle("Options", displayMode: .inline)
            } else {
               Text(options[$0])
            }
        }
    }
}
.navigationBarTitle("Settings", displayMode: .inline)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690