Philipp
Philipp

Reputation: 347

Segmented style picker alignment in NavigationBarItems

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?

Upvotes: 2

Views: 231

Answers (1)

Asperi
Asperi

Reputation: 257711

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

Upvotes: 1

Related Questions