UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Trailing closure passed to parameter of type 'Visibility' that does not accept a closure

Forums > SwiftUI

Hi All, I'm quite new to swift and am wondering why I am receiving a "Trailing closure passed to parameter of type 'Visibility' that does not accept a closure" error on the .toolbar { line I Have Supplied My Code Below Thanks in Advance, Josh

import SwiftUI

struct HomeView: View {

    var body: some View {

        Text("Home")

            .toolbar {

                ToolbarItem(placement: .navigationBarLeading)

                Button(action: NavigationLink(destination: AccountView()), label: Image(systemName: "gearshape"))

                ToolbarItem(placement: .principal) { Text("Welcome").font(.title)

                }

                ToolbarItem(placement: .navigationBarTrailing)

                Button(action: NavigationLink(destination: AccountView()), label: Image(systemName: "person.circle"))

            }

            .navigationBarTitleDisplayMode(.inline)

    }

}

struct HomeView_Previews: PreviewProvider {

    static var previews: some View {

        HomeView()

    }

}

1      

You are missing the { from ToolbarItem Also you do not need Button with NavigationLink

var body: some View {
        NavigationStack {
            Text("Home")
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        NavigationLink {
                            AccountView()
                        } label: {
                            Image(systemName: "gearshape")
                        }

                    }
                }
        }
    }

1      

In Xcode you can either click or double click on the curly brackets { } to see their counterpart.

Clicking will light up the counterpart, Double clicking will mark the whole block. This might be helpful to spot these kind of errors.

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.