【问题标题】:Segmented style picker alignment in NavigationBarItemsNavigationBarItems 中的分段样式选择器对齐
【发布时间】:2020-12-25 10:38:31
【问题描述】:

我现在正致力于将分段样式选择器放入 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:,则选择器显示在右侧。我怎样才能把它放在中心

【问题讨论】:

    标签: ios swift swiftui navigationbar


    【解决方案1】:

    改用.toolbar,比如

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      相关资源
      最近更新 更多