【问题标题】:How can I place a SegmentedControl inside the center of the Navigation Bar by using SwiftUI?如何使用 SwiftUI 将 SegmentedControl 放置在导航栏的中心?
【发布时间】:2019-11-17 16:36:46
【问题描述】:

我想使用 SwiftUI 在 NavigationBar 的中心放置一个 SegmentedControl 或一个 Button。我可以将它放在尾随和前导部分,但不能放在中心。

【问题讨论】:

    标签: swift xcode swiftui xcode11 ios13


    【解决方案1】:

    目前这是不可能的。 navigationBarTitle(...) 仅接受 TextDocumentation here.

    【讨论】:

      【解决方案2】:

      在 SwiftUI 2.0、Xcode 12 中,您可以实现如下:

      
      VStack {
          ....
      }
      .toolbar {
          ToolbarItem(placement: .principal) {
              Picker(selection: $selectionIndex,
                     label: EmptyView()) {
                  Text("Option 1").tag(0)
                  Text("Option 2").tag(1)
               }
               .labelsHidden()
               .pickerStyle(SegmentedPickerStyle())
           }
                  
           ToolbarItem(placement: .navigationBarLeading) {
               leadingNavigationBarItem
           }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-02-02
        • 2013-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        • 1970-01-01
        • 2020-05-07
        相关资源
        最近更新 更多