简体   繁体   中英

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())
            )
        }
    }
}

在此处输入图像描述

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 ?

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())
    }
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM