Adding a Toggle to the toolbar

Hi everyone!

I am trying to add a toggle to the top toolbar in a Navigation View. What I end up getting is a small rounded rectangle with the toggle label inside the rectangle. I've attached a simple bit of code that shows the problem. Any clues what I'm doing wrong?

I'm using Xcode 13.4.1 The toggle doesn't show correctly in the preview, the simulator, or on my iPhone 12 mini running iOS 15.5

import SwiftUI

struct ContentView: View {

    var body: some View {

        NavigationView {
            List {
            NavigationLink(destination: Text("Destination"))  {
                     Text("Navigate")
                 }
            }
            .navigationTitle("Test")
            .toolbar {
                Toggle(isOn: .constant(true)) {
                      Text("Label")
                 }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {
        ContentView()
    }
}
Answered by DTS Engineer in 722157022

Many controls have a different default appearance in a toolbar. It looks like the default for a toolbar is .button. On iOS, you can use .switch instead:

                Toggle(isOn: .constant(true)) {
                    Text("Label")
                }.toggleStyle(.switch)
Accepted Answer

Many controls have a different default appearance in a toolbar. It looks like the default for a toolbar is .button. On iOS, you can use .switch instead:

                Toggle(isOn: .constant(true)) {
                    Text("Label")
                }.toggleStyle(.switch)
Adding a Toggle to the toolbar
 
 
Q