NavigationBarItems 中的分段样式选择器对齐方式 [英] Segmented style picker alignment in NavigationBarItems

查看:50
本文介绍了NavigationBarItems 中的分段样式选择器对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正致力于将分段样式选择器放在 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())
            )
        }
    }
}

如果我使用 leading: 选择器显示在左边,如果我使用 trailing: 那么选择器显示在右边.我怎样才能把它放在中心?

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?

推荐答案

使用 .toolbar 代替,如

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

这篇关于NavigationBarItems 中的分段样式选择器对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆