2

I'm now working on putting segmented style picker inside NavigationBarItems. Here's the code I'm using:

struct ContentView: View {
    let modes = ["temperature", "distance"]

    var body: some View {
        NavigationView {
            ZStack {
                ...
                }
            }
            .navigationBarItems (leading:
                                    Picker ("Select mode:", selection: $currentMode) {
                                        ForEach (0..<mods.count) {
                                            Text(self.mods[$0])
                                        }
                                    }
                                    .pickerStyle(SegmentedPickerStyle())
            )
        }
    }
}

enter image description here

If I use leading: the picker is shown on the left, if I use trailing: then the picker is shown on the right. How can I put it in the centre?

1 Answer 1

1

Use .toolbar instead, like

ZStack {
    Text("Demo")
}
.toolbar {
    ToolbarItem(placement: .principal) {
        Picker ("Select mode:", selection: $currentMode) {
            ForEach (0..<modes.count) {
                Text(self.modes[$0])
            }
        }
        .pickerStyle(SegmentedPickerStyle())
    }
}

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.