GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: 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      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.

Click to save your free spot now

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.