Viktor Maric
Viktor Maric

Reputation: 603

How can I place a SegmentedControl inside the center of the Navigation Bar by using SwiftUI?

I would like to place a SegmentedControl or just a Button inside the center of the NavigationBar by using SwiftUI. I could place it on the trailing and leading part but not in the center.

Upvotes: 1

Views: 739

Answers (2)

Phillip
Phillip

Reputation: 871

In SwiftUI 2.0, Xcode 12, You could achieve like below:


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

Upvotes: 2

arsenius
arsenius

Reputation: 13256

This is not currently possible. navigationBarTitle(...) only accepts Text. Documentation here.

Upvotes: 0

Related Questions