我现在正在努力将分段样式选择器放入NavigationBarItems中。下面是我使用的代码:
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())
)
}
}
}

如果使用leading:,则选择器显示在左侧,如果使用trailing:,则选择器显示在右侧。我怎样才能把它放在中心?
发布于 2020-12-25 11:18:33
使用.toolbar代替,如
ZStack {
Text("Demo")
}
.toolbar {
ToolbarItem(placement: .principal) {
Picker ("Select mode:", selection: $currentMode) {
ForEach (0..<modes.count) {
Text(self.modes[$0])
}
}
.pickerStyle(SegmentedPickerStyle())
}
}

https://stackoverflow.com/questions/65447317
复制相似问题